简体   繁体   English

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

[英]Java JTree freezing when file is re-selected

I have a JTree that I use as a file tree. 我有一个JTree,我用它作为文件树。 If I choose a new file, and select the same one as was already selected, the tree freezes up for some reason. 如果我选择一个新文件,然后选择与已经选择的文件相同的文件,则由于某种原因,树将冻结。 It should be removing the old JScrollPane containing the tree and replacing it with a new one, and it works fine if I select a different file, but not with the same one. 它应该删除包含该树的旧JScrollPane并将其替换为新树,并且如果我选择其他文件但不能使用同一文件,则可以正常工作。 The rest of the GUI still works, it's just the tree that freezes. GUI的其余部分仍然有效,只是冻结的树。 Here is the relevant code: 这是相关代码:

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;
            }
        }

    }

I can supply the code for the FileTree class, as well, if that is needed. 如果需要,我也可以提供FileTree类的代码。

It should be removing the old frame and replacing it with a new one 应该删除旧框架,然后换上新框架

You can't add/remove a JFrame from a JFrame so I don't know what that comment means. 您无法在JFrame中添加/删除JFrame,因此我不知道该注释的含义。

Don't remove/add components? 不删除/添加组件? If you want to update an existing component then change the model. 如果要更新现有组件,请更改模型。 That is: 那是:

tree.setModel(...);

Or if you do remove/add components, then you need to use: 或者,如果确实要删除/添加组件,则需要使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM