有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

重新选择文件时swing Java JTree冻结

我有一个JTree用作文件树。如果我选择了一个新文件,并选择了与已选择的文件相同的文件,那么由于某种原因,树会冻结。它应该删除包含树的旧JScrollPane,并用一个新的替换它,如果我选择一个不同的文件,但不是同一个文件,它就可以正常工作。GUI的其余部分仍在工作,只是树冻结了。以下是相关代码:

if ("browse".equals(e.getActionCommand())) {
        returnVal = fc.showOpenDialog(DSAuto.this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            filename = file.getAbsolutePath();
            l1.setText("Job Location: " + filename);
            c.fill = GridBagConstraints.BOTH;
            c.weightx = 1.0;
            c.gridx = 0;
            c.gridy = 1;
            c.gridwidth = 10;
            c.gridheight = 9;
            c.ipady = 0;
            if (rm)
                pane.remove(ft1);
            else
                pane.remove(sp1);
            if (rm2) {
                pane.remove(l3);
                rm2 = false;
            }
            if (!(file.isDirectory() || file.isFile())) {
                l3 = new JLabel("404 File Not Found");
                pane.add(l3, c);
                rm2 = true;
            } else {
                ft1 = new FileTree(file);
                ft1.all = allB;
                pane.add(ft1, c);
                rm = true;
            }
        }

    }

如果需要,我也可以提供FileTree类的代码


共 (1) 个答案

  1. # 1 楼答案

    It should be removing the old frame and replacing it with a new one

    你不能从JFrame中添加/删除JFrame,所以我不知道这个评论是什么意思

    不删除/添加组件?如果要更新现有零部件,请更改模型。也就是说:

    tree.setModel(...);
    

    或者,如果确实要删除/添加组件,则需要使用:

    panel.revalidate();
    panel.repaint();