我们如何使用 select 和悬停事件动态更改 jtree 的叶节点的文本?

How can we change the text of leaf node of jtree dynamically withour select and hover event?

我的要求是在没有 select 和悬停事件的情况下更改叶节点文本的颜色。

示例:

如果我在输入框中输入 java 并单击按钮,则 Onclick 事件 Java 叶节点文本变为红色。

据我所知,自定义 JTree 显示的唯一方法是通过其单元格渲染器。因此,在下面的代码中,我创建了一个自定义单元格渲染器,它在激活按钮后更改请求节点的文本颜色。

请注意,以下代码的唯一目的是演示如何实现您的要求。它并不意味着是一个生产质量的程序。我的希望和意图是,它可以帮助您完成所需的任务。

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());
    }
}