You can easily change the font attributes like bold and italic of a JTree‘s tree node (parent or leaf node) by overriding the DefaultTreeCellRenderer class getTreeCellRendererComponent() method. Then you can set the JTree’s setRenderer() method to this renderer object.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class MyTreeCellRenderer extends DefaultTreeCellRenderer { @Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus); if (selected) { setFont(getFont().deriveFont(Font.BOLD + Font.ITALIC)); } else setFont(getFont().deriveFont(Font.PLAIN)); return this; } } |
The code above sets the tree node’s font attributes (in this case bold and italic) if it is selected. If you want all tree nodes to have these same font attributes, you can remove the if condition statement.