如何在不涉及选择和悬停事件的情况下动态更改jtree的叶节点文本?

huangapple go评论147阅读模式
英文:

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

问题

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

示例:

如果我在输入框中输入“java”,然后点击按钮,点击事件后,Java叶节点文本变为红色。

英文:

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.

如何在不涉及选择和悬停事件的情况下动态更改jtree的叶节点文本?

答案1

得分: 3

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
    public void actionPerformed(ActionEvent event) {
        String actionCommand = event.getActionCommand();
        switch (actionCommand) {
            case CLICK:
                changeColor = true;
                tree.repaint();
                break;
            default:
        }
    }

    @Override
    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
    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());
    }
}
英文:

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

huangapple
  • 本文由 发表于 2020年4月10日 13:46:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/61134679.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定