0

My requirement is to change the color of leaf node text without select and hover event.

Example:

If I input java in inputbox and click on button, than Onclick event Java leaf node text become red.

enter image description here

Bhavin S.
  • 77
  • 12
  • 1
    So maybe [edit] your question, post your code and indicate where, in the code, you are having difficulty. – Abra Apr 10 '20 at 05:50
  • 1
    Hint: Create a custom renderer. Refer to [How to Use Trees](https://docs.oracle.com/javase/tutorial/uiswing/components/tree.html) which is part of Oracle's java tutorials. – Abra Apr 10 '20 at 06:22
  • 2
    Does this help: [Change the color of one or more nodes in Jtree dynamically](https://stackoverflow.com/questions/8652862/change-the-color-of-one-or-more-nodes-in-jtree-dynamically) – Abra Apr 10 '20 at 06:26
  • Thanks for comment @Abra I want to travers jtree and check if leaf node text is "Java " then change color of the leaf node text to red. – Bhavin S. Apr 10 '20 at 06:37
  • *"I want to.."* I see no need to traverse anything. If the content / value of the node is "Java", adjust the color in the renderer. Though I might misunderstand the requirement, since that seems less 'dynamic' than changing the rendering on hover. For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Hard code data for the tree. BTW - just saw the edit with image. There is nothing in the image that could not be understood by reading the text. **Edit - my bad:** I now realise the user (I guess) types a text in and presses a button .. – Andrew Thompson Apr 10 '20 at 08:18
  • .. to get the text to change? OK .. Once the button is pressed, maybe store the value and render the tree again, having the renderer check the value for whichever text is required to be made red. But .. post an MRE / SSCE if you cannot figure it out. – Andrew Thompson Apr 10 '20 at 08:21

1 Answers1

3

As far as I know, the only way to customize the display of a JTree is via its cell renderer. Hence, in the below code, I have created a custom cell renderer that changes the text color of the requested node, after activating the button.

Note that the only purpose of the below code is to demonstrate how to achieve what you require. It is not meant to be a production-quality program. My hope and intention is that it gets you going towards completing your required task.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.WindowConstants;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;

public class TreeTest extends DefaultTreeCellRenderer implements ActionListener, Runnable {
    private static final String CLICK = "Click";

    private boolean changeColor;
    private JFrame frame;
    private JTextField textField;
    private JTree tree;

    @Override // java.awt.event.ActionListener
    public void actionPerformed(ActionEvent event) {
        String actionCommand = event.getActionCommand();
        switch (actionCommand) {
            case CLICK:
                changeColor = true;
                tree.repaint();
                break;
            default:
        }
    }

    @Override // javax.swing.tree.DefaultTreeCellRenderer
    public Component getTreeCellRendererComponent(JTree tree,
                                                  Object value,
                                                  boolean sel,
                                                  boolean expanded,
                                                  boolean leaf,
                                                  int row,
                                                  boolean hasFocus) {
        Component cmpt = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
        Object usrObj = node.getUserObject();
        if (textField != null && textField.getText().equals(usrObj) && changeColor) {
            cmpt.setForeground(Color.RED);
        }
        return cmpt;
    }

    @Override // java.lang.Runnable
    public void run() {
        createAndShowGui();
    }

    private void createAndShowGui() {
        frame = new JFrame("JTree");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(createTree(), BorderLayout.CENTER);
        frame.add(createTopPanel(), BorderLayout.PAGE_START);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel createTopPanel() {
        JPanel topPanel = new JPanel();
        JButton button = new JButton(CLICK);
        button.addActionListener(this);
        textField = new JTextField(6);
        topPanel.add(textField);
        topPanel.add(button);
        return topPanel;
    }

    private JScrollPane createTree() {
        tree = new JTree(createTreeModel());
        tree.setCellRenderer(this);
        JScrollPane scrollPane = new JScrollPane(tree);
        return scrollPane;
    }

    private TreeModel createTreeModel() {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Language");
        root.add(new DefaultMutableTreeNode("Java"));
        root.add(new DefaultMutableTreeNode("C"));
        root.add(new DefaultMutableTreeNode("C++"));
        DefaultTreeModel model = new DefaultTreeModel(root);
        return model;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new TreeTest());
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41