The colors (both foreground and background) for your tree nodes come from the TreeCellRenderer
that is associated with your JTree
. The tree cell renderer for your JTree
depends on the look and feel, but you can probably assume that it is descended from DefaultTreeCellRenderer
. If it is, then you can call several color-setting methods on DefaultTreeCellRenderer
to change the colors of your tree, like this:
tree = new JTree(root);
if (tree.getCellRenderer() instanceof DefaultTreeCellRenderer)
{
final DefaultTreeCellRenderer renderer =
(DefaultTreeCellRenderer)(tree.getCellRenderer());
renderer.setBackgroundNonSelectionColor(Color.YELLOW);
renderer.setBackgroundSelectionColor(Color.ORANGE);
renderer.setTextNonSelectionColor(Color.RED);
renderer.setTextSelectionColor(Color.BLUE);
}
else
{
System.err.println("Sorry, no special colors today.");
}