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

52 Views Asked by At

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

0

There are 0 best solutions below