How can I change the text color to certain elements of JTree but not to the whole tree?

I know that I can change the text color using:

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.awt.*;

public class UIManagerNodeColor {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("UIManager Node Color");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


            UIManager.put("Tree.textForeground", Color.decode("#6FB2D2"));

            JTree tree = new JTree();

            DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
            DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Node 1");
            DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Node 2");

            root.add(node1);
            root.add(node2);


            tree.setModel(new DefaultTreeModel(root));

            frame.add(new JScrollPane(tree));
            frame.setSize(300, 200);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}

this is the output:

By doing this all the node’s text color is changed, but how should I do to change only the color of "Node 1"?

>Solution :

To change the text color of certain elements in a JTree in Java Swing, you can use a custom renderer. A renderer is responsible for rendering the visual representation of the nodes in the tree. Here’s how you can achieve this:

  1. Create a custom cell renderer class that extends DefaultTreeCellRenderer:
import javax.swing.*;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.*;

public class CustomTreeCellRenderer extends DefaultTreeCellRenderer {
    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value,
                                                  boolean selected, boolean expanded,
                                                  boolean leaf, int row, boolean hasFocus) {
        Component component = super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);

        // Check if the element's text should have a different color
        if (value.toString().equals("ElementName")) { // Replace with the actual condition
            component.setForeground(Color.RED); // Change the color as needed
        } else {
            component.setForeground(Color.BLACK); // Default color
        }

        return component;
    }
}
  1. Set the custom renderer to your JTree:

Assuming you have a JTree called tree, you can set the custom renderer like this:

tree.setCellRenderer(new CustomTreeCellRenderer());
  1. Customize the condition inside the getTreeCellRendererComponent method to determine which elements should have their text color changed. In the example above, I’ve used "ElementName" as a placeholder condition. Replace it with the actual condition that determines which elements should have their text color changed.

Remember that this approach only affects the text color of specific tree nodes, not the entire tree. It allows you to apply custom rendering only to the elements that meet the condition you define in the getTreeCellRendererComponent method.

Leave a Reply