JTree刷新而不折叠

JTree refresh without collapsing

我在刷新 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 个节点。如果我们点击按钮 "Rebuild",树将不会更新它自己。奇怪的是,如果我们不在开始时展开 "Root" 节点(所以只需启动应用程序)并单击按钮,然后展开 "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});
    }
});