使用`nodeChanged`更新树节点是否合适? [英] Is it proper to update a tree node using `nodeChanged`

查看:63
本文介绍了使用`nodeChanged`更新树节点是否合适?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JTreeJTextField.当我在树上选择一个节点时,文本字段将显示该节点的值.然后,我可以编辑文本并将其保存以更改选定节点的值.我使用DefaultTreeModelnodeChanged方法来更新树.

I have a JTree and a JTextField. When I select a node on the tree, the text field will display the value of the node. I can then edit the text and save it to change the selected node's value. I use the DefaultTreeModel's nodeChanged method to update the tree.

这是告诉树模型更新其节点的正确方法吗?在我看来,这很丑陋,因为我正在显式访问树的模型,并告诉它发生了什么事情.

Is this a proper way to tell the tree model to update its node? To me it looks ugly because I'm explicitly accessing the tree's model and telling it that something has happened.

这是一些代码

import java.awt.BorderLayout;
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.ScrollPaneConstants;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public class TextPaneTest extends JFrame {

    private JTextField textBox = null;
    private JTree tree = null;
    private JButton button = null;

    public static void main(String args[]) {
        new TextPaneTest();
    }

    public TextPaneTest() {

        // Main panel
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        // Panel holding tree
        JPanel treePanel = new JPanel();
        treePanel.setLayout(new BorderLayout());

        // Panel holding text field and button

        JPanel editPanel = new JPanel();
        editPanel.setLayout(new BorderLayout());

        // My textbox
        textBox = new JTextField();

        // button
        button = new JButton();
        button.setText("Save changes");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
                String text = textBox.getText();
                node.setUserObject(text);
                DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
                model.nodeChanged(node);
            }
        });

        // My Tree
        DefaultMutableTreeNode top = new DefaultMutableTreeNode("Root");
        tree = new JTree(top);
        tree.addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
                textBox.setText(node.getUserObject().toString());
        }
        });

        JScrollPane scrollPane = new JScrollPane(tree);
        scrollPane.setHorizontalScrollBarPolicy(
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        // Add tree
        treePanel.add(scrollPane);
        panel.add(treePanel, BorderLayout.CENTER);

        // Add box and button to edit panel
        editPanel.add(textBox, BorderLayout.CENTER);
        editPanel.add(button, BorderLayout.SOUTH);

        // Add edit panel
        panel.add(editPanel, BorderLayout.SOUTH);

        // Add everything to the frame
        this.add(panel);
        this.setSize(300, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
}

推荐答案

通过遍历JavaDoc和DefaultTreeModel和DefaultMutableTreeNode的代码,我认为您正在做的事情没问题.如果您更改了模型的结构(例如,通过删除节点),则无需在模型上调用任何内容,因为TreeModel知道您已对其执行了某些操作.但是,在这种情况下,您将更改模型引用的节点的内容.因此,您必须通知TreeModel它的一个节点的内容现在有所不同,这似乎是合理的.

From poking around the JavaDoc and code for DefaultTreeModel and DefaultMutableTreeNode, I think what you are doing is ok. If you had changed the structure of the model (say by removing a node) then you would not need to call anything on the model, since the TreeModel would know that you did something to it. However, in this case, you are changing the contents of a node that the model references. So it seems reasonable that you would have to inform the TreeModel that the contents of one of its nodes is now different.

我本人可能会想念一些东西,但我认为您的情况还可以.

I may be missing something myself, but I think what you have is fine.

这篇关于使用`nodeChanged`更新树节点是否合适?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆