I have created a JTree for my application. Now I want to change the color of some nodes or of complete tree itself. I searched and found to create a custom TreeCellRendererComponent and update the color in it but it is not working for me. Maybe I am missing something or what I am thinking is maybe I'm updating certain property of JTree which is causing the color to not change.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.border.EmptyBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
public class TestClass2 extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestClass2 frame = new TestClass2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TestClass2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout());
contentPane.add(prepareCommandTree(), BorderLayout.CENTER);
}
public static JTree prepareCommandTree()
{
// Root node name which is the name of the command e.g. Display Text
DefaultMutableTreeNode commandNode = new DefaultMutableTreeNode("Command Name");
DefaultMutableTreeNode completeData = new DefaultMutableTreeNode("Complete Data");
// Adding all branches under root branch
commandNode.add(completeData);
//create the tree by passing in the root node
JTree commandTree = new JTree(commandNode);
DefaultTreeModel model = (DefaultTreeModel)commandTree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();
model.reload(root);
// Setting JTree background
commandTree.setOpaque(false);
commandTree.collapseRow(0);
commandTree.setBorder(new EmptyBorder(5, 0, 0, 0));
commandTree.setFont( new Font(Font.MONOSPACED, Font.PLAIN, 13));
// Adding image icon to the tree
commandTree.setCellRenderer(new DefaultTreeCellRenderer(){
private static final long serialVersionUID = 1L;
public Component getTreeCellRendererComponent(final JTree tree,Object value,
boolean sel,boolean expanded,boolean leaf,int row,boolean hasFocus)
{
// Trying to change color of tree
setForeground(Color.RED);
JLabel label = (JLabel)super.getTreeCellRendererComponent(tree,value,
sel,expanded,leaf,row,hasFocus);
return label;
}
});
// Setting adjustments to JTree properties
commandTree.putClientProperty("JTree.lineStyle", "None");
commandTree.setAlignmentX(Component.LEFT_ALIGNMENT);
DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) commandTree.getCellRenderer();
renderer.setLeafIcon(null);
renderer.setClosedIcon(null);
renderer.setOpenIcon(null);
return commandTree;
}
}
Any suggestion/correction will be helpful. Thanks. :-)
Your renderer sets its foreground Color
before the super
call has been performed.
The super
method also sets a foreground Color
, which will replace the one you have set.
Just apply the Color
after the super
call .
public Component getTreeCellRendererComponent(final JTree tree, final Object value,
final boolean sel, final boolean expanded, final boolean leaf, final int row,
final boolean hasFocus) {
// Trying to change color of tree
JLabel label = (JLabel) super.getTreeCellRendererComponent(tree, value,
sel, expanded, leaf, row, hasFocus);
setForeground(Color.RED);
return label;// Or "return this", since the method actually returns the renderer component itself
}
});