Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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"?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading