java - JTree刷新而不崩溃

标签 java jtree

我在刷新 JTree 实例时遇到问题。请参阅以下代码:

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public class ItemTest {

    private JFrame frame;
    private DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    private JTree tree = new JTree(root);
    private JButton button = new JButton("Rebuild");
    private String[] array = new String[] { "name", "first_name", "middle_name", "last_name"};

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    ItemTest window = new ItemTest();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public ItemTest() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 523, 349);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JScrollPane scrollBar = new JScrollPane();
        GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(scrollBar, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE)
                        .addComponent(button, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(412, Short.MAX_VALUE))
                );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                        .addGap(22)
                        .addComponent(scrollBar, GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)
                        .addComponent(button, GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
                        .addContainerGap())
                );
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                root.add(new DefaultMutableTreeNode("Row 4"));
                ((DefaultTreeModel) tree.getModel()).nodeChanged(root);
            }
        });
        for(String string : array) {
            root.add(new DefaultMutableTreeNode(string));
        }
        scrollBar.setViewportView(tree);
        frame.getContentPane().setLayout(groupLayout);
    }
}

如果我们运行此代码,并展开“Root”节点。我们将在其中看到 4 个节点。如果我们单击“重建”按钮,树将不会更新它自己。奇怪的是,如果我们在开始时不展开“Root”节点(因此只需启动应用程序)并单击按钮,然后展开“Root”节点,就会添加新行。有谁知道如何刷新这棵树而不崩溃,因为当您在开始时展开“根”节点时,nodeChanged 似乎不起作用。

注意:我必须在不使用 insertNodeInto 的情况下完成此操作。

最佳答案

您必须通知监听器已插入节点:

button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent arg0) {
        root.add(new DefaultMutableTreeNode("Row 4"));
        ((DefaultTreeModel) tree.getModel()).nodesWereInserted(
                root, new int[]{root.getChildCount()-1});
    }
});

关于java - JTree刷新而不崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29518525/

相关文章:

java - Exception 对象上的 getMessage() 不提供 null

java - 如何在java appium中获取完整的元素列表(包括滚动时可见的元素)?

java - JTree 项中的 setPreferredSize 为零会导致渲染性能不佳

java - 是什么导致任意 JTree 节点错误地显示它们的名称,我该如何阻止它发生?

java - MySQL RDS 和 JDBC SSL 连接给出错误 : unable to find valid certification path to requested target

java - 开源Java游戏的一个好例子是什么?

java - 在JAVA中强制浏览器下载docx文件会生成损坏的文档

java - SQL : WHERE not working with JTable

java - 默认选择节点

java - 渲染时更改 JTree 行高调整行为