当前位置: 首页 > 知识库问答 >
问题:

JTree避免重新加载后崩溃

满自明
2023-03-14

我正在试图找到一个解决方案,解决重新加载后在JTree中崩溃的问题。情况:

树形结构

[-] Office A
 |---[-] Office A.1
 |    |---[-] Office A.1.1
 |    |---[-] Office A.1.2
[-] Office B
 |---[-] Office B.1
 |    |---[-] Office B.1.1
 |    |    |---[-] Office B.1.1.1

现在我必须添加Office A.1.3。为此,我得到了Office A.1,并使用方法add(DefaultMutableTreeNode阳极)I添加Office A.1.3

OfficeA1.add(OfficeA13);

在这之后,我调用树的DefaultTreeModel上的reload方法。

问题是,在这个调用之后,树将全部崩溃:

[+] Office A
[+] Office B

而且我必须手动展开节点Office A以确保节点被添加...

[-] Office A
 |---[-] Office A.1
 |    |---[-] Office A.1.1
 |    |---[-] Office A.1.2
 |    |---[-] Office A.1.3
[+] Office B

我的密码。。。

   DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root not visibile");
   DefaultMutableTreeNode usersRoot = new DefaultMutableTreeNode("Utenti");
   DefaultMutableTreeNode groupsRoot = new DefaultMutableTreeNode("Gruppi");
   DefaultMutableTreeNode officesRoot = new DefaultMutableTreeNode("Uffici")
   root.add(usersRoot);
   root.add(groupsRoot);
   root.add(officesRoot);

   JTree ccTree = new JTree(root);

当我添加节点时。。。

Office anOffice = //get the correct office object
DefaultTreeModel model = (DefaultTreeModel)competenzaTree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();

DefaultMutableTreeNode n = (DefaultMutableTreeNode)root.getChildAt(0);

n.add(new DefaultMutableTreeNode(anOffice));
model.reload(n);

问题在于officeroot节点的位置。usersRootgroupsRoot节点没有层次结构。

有没有办法避免这种行为?谢谢

也许另一种方法可以问是,在不导致所有树崩溃的情况下,从树中添加/删除节点的方法是什么?

p、 我也读过这篇文章,但对我没有帮助。

共有1个答案

林博厚
2023-03-14

考虑使用DefaultTreeModel#插入节点(DefaultMutableNode, DefaultMutableNode, int),它应该通知JTree表有一个新节点可用,导致更新JTree,但不应该影响树的当前展开状态

示例基于如何使用树中的示例

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

public class TestTree extends JPanel {

    private JTree tree;
    private DefaultTreeModel model;
    private JButton btnAdd;
    private int childCount;

    public TestTree() {
        super(new BorderLayout());

        //Create the nodes.
        DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series");
        createNodes(top);

        model = new DefaultTreeModel(top);

        //Create a tree that allows one selection at a time.
        tree = new JTree(model);
        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

        //Create the scroll pane and add the tree to it. 
        JScrollPane treeView = new JScrollPane(tree);

        //Add the split pane to this panel.
        add(treeView);

        btnAdd = new JButton("Add");
        btnAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                TreePath treePath = tree.getSelectionPath();
                if (treePath != null) {
                    DefaultMutableTreeNode node = (DefaultMutableTreeNode) treePath.getLastPathComponent();
                    DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child " + (++childCount));
                    model.insertNodeInto(child, node, node.getChildCount());
                }
            }
        });

        add(btnAdd, BorderLayout.SOUTH);
    }

    private class BookInfo {

        public String bookName;

        public BookInfo(String book) {
            bookName = book;
        }

        public String toString() {
            return bookName;
        }
    }

    private void createNodes(DefaultMutableTreeNode top) {
        DefaultMutableTreeNode category = null;
        DefaultMutableTreeNode book = null;

        category = new DefaultMutableTreeNode("Books for Java Programmers");
        top.add(category);

        //original Tutorial
        book = new DefaultMutableTreeNode(new BookInfo("The Java Tutorial: A Short Course on the Basics"));
        category.add(book);

        //Tutorial Continued
        book = new DefaultMutableTreeNode(new BookInfo("The Java Tutorial Continued: The Rest of the JDK"));
        category.add(book);

        //JFC Swing Tutorial
        book = new DefaultMutableTreeNode(new BookInfo("The JFC Swing Tutorial: A Guide to Constructing GUIs"));
        category.add(book);

        //Bloch
        book = new DefaultMutableTreeNode(new BookInfo("Effective Java Programming Language Guide"));
        category.add(book);

        //Arnold/Gosling
        book = new DefaultMutableTreeNode(new BookInfo("The Java Programming Language"));
        category.add(book);

        //Chan
        book = new DefaultMutableTreeNode(new BookInfo("The Java Developers Almanac"));
        category.add(book);

        category = new DefaultMutableTreeNode("Books for Java Implementers");
        top.add(category);

        //VM
        book = new DefaultMutableTreeNode(new BookInfo("The Java Virtual Machine Specification"));
        category.add(book);

        //Language Spec
        book = new DefaultMutableTreeNode(new BookInfo("The Java Language Specification"));
        category.add(book);
    }

    /**
     * Create the GUI and show it. For thread safety, this method should be
     * invoked from the event dispatch thread.
     */
    private static void createAndShowGUI() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        }

        //Create and set up the window.
        JFrame frame = new JFrame("TreeDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add content to the window.
        frame.add(new TestTree());

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

}
 类似资料:
  • 我使用DataTables编辑器插件(https://editor.datatables.net/)用于编辑包含远程数据的表(https://datatables.net/examples/data_sources/server_side.html)在内联模式下(https://editor.datatables.net/examples/inline-editing/simple.html) 在

  • 我有一个使用文本输入的表单和一个将文本添加到数据库表的控制器 我使用ajax调用这个函数,如下所示: 数据已成功插入。然而,当控制器试图重定向到时,我遇到了一个错误 我的背景。xml有以下条目 我尝试将addStuff的返回类型更改为void。然而,我收到了相同的错误消息。 如何在不重定向或刷新页面的情况下插入到数据库?

  • 刷新JTree实例时出现问题。请参见以下代码: 如果我们运行这段代码,并展开“根”节点。我们将在其中看到4个节点。如果我们单击按钮“重建”,树将不会更新它的自。奇怪的是,如果我们在开始时不展开“根”节点(所以只是启动应用程序)并单击按钮,之后我们展开“根”节点,新行被添加。有人知道如何在不崩溃的情况下刷新这棵树吗,因为当你在开始时展开“根”节点时,nodeChanged似乎不起作用。 注意:我必须

  • 我有一个反应应用程序开发的底漆-反应模板。它有一个管理仪表板,在仪表板侧板中有几条路由。我已经在我的index.js中设置了一个身份验证路由,当我访问每条路由时,它都运行得很好。但是当重新加载页面时,页面显示404页面找不到! 我保护“/”路径不受AUthrote的影响,这样每个以“/”开头的路径都受到保护(我在仪表板中有路由,如“/usertable”、“/users”、“/payments”等

  • 我正在对我的Laravel控制器进行简单验证: 我的问题是,如果失败,这个验证会将我重定向到主,我通过AJAX发出请求,我知道Laravel会检测到通过Ajax发出的请求,但它只有在正常请求(我发送的典型请求标题与内容类型应用程序/json和正文中我发送了一个正常的JSON 但是Laravel无法检测到当te Ajax请求不是时,我使用的是JavaScript的对象,因此,我没有发送标题,而是在正