在运行时重新加载 JTree

发布于 2024-07-17 17:17:07 字数 725 浏览 7 评论 0原文

我在独立于 GUI 类的类中创建了一个 JTree 和模型。 JTree 的数据是从文件中提取的。

现在,在 GUI 类中,用户可以将文件从文件系统添加到 AWT 列表。 用户单击列表中的文件后,我希望更新 JTree。 JTree 的变量名称是 schemaTree。

当选择列表中的项目时,我有以下代码:

private void schemaListItemStateChanged(java.awt.event.ItemEvent evt) {
        int selection = schemaList.getSelectedIndex();
        File selectedFile = schemas.get(selection);
        long fileSize = selectedFile.length();
        fileInfoLabel.setText("Size: " + fileSize + " bytes");

        schemaParser = new XSDParser(selectedFile.getAbsolutePath());

        TreeModel model = schemaParser.generateTreeModel();
        schemaTree.setModel(model);
}

我已更新代码以对应于接受的答案。 JTree 现在根据我在列表中选择的文件正确更新。

I create a JTree and model for it out in a class separate to the GUI class. The data for the JTree is extracted from a file.

Now in the GUI class the user can add files from the file system to an AWT list. After the user clicks on a file in the list I want the JTree to update. The variable name for the JTree is schemaTree.

I have the following code for the when an item in the list is selected:

private void schemaListItemStateChanged(java.awt.event.ItemEvent evt) {
        int selection = schemaList.getSelectedIndex();
        File selectedFile = schemas.get(selection);
        long fileSize = selectedFile.length();
        fileInfoLabel.setText("Size: " + fileSize + " bytes");

        schemaParser = new XSDParser(selectedFile.getAbsolutePath());

        TreeModel model = schemaParser.generateTreeModel();
        schemaTree.setModel(model);
}

I've updated the code to correspond to the accepted answer. The JTree now updates correctly based on which file I select in the list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画尸师 2024-07-24 17:17:07

来自 < code>Component.add API 文档。

注意:如果组件已添加到
已显示的容器,
必须对此调用 validate
用于显示新内容的容器
成分。 如果有多个组件
添加后,您可以改进
仅调用 validate 来提高效率
一次,在所有组件都完成之后
已添加。

您对未显示的组件调用了 repaintvalidate,这不会生效。 您需要在 add 之后调用 mainPanel 上的这些方法。 此外,revalidate 往往比 validate 更好,因为它有效地合并。

From the Component.add API docs.

Note: If a component has been added to
a container that has been displayed,
validate must be called on that
container to display the new
component. If multiple components are
being added, you can improve
efficiency by calling validate only
once, after all the components have
been added.

You have called repaint and validate on a component that is not displayed, which will not be effective. You need to call those methods on the mainPanel after the add. Also revalidate tends to be better than validate as it effectively coalesces.

梦里南柯 2024-07-24 17:17:07

我不确定我是否理解你的问题,但我会尝试...

正确的做法应该是,恕我直言:

  • 获取文件
  • 从文件中创建一个新的 TreeModel
  • 将模型提供给 JTree

在伪代码中,它会看起来像这样:

File newContent = getSelectedByUser(...);
TreeModel newModel = new MyFileBasedTreeModel(newContent);
//this next part must be done in the EventDispatcherThread
myTree.setModel(newModel); 

然后 JTree 将被更新,没有任何调用重新绘制等。

希望它有帮助

I not sure that I'm understanding your question, but I'll try...

The right thing to do should be, IMHO:

  • get the file
  • create a new TreeModel from your file
  • give the model to the JTree

In pseudocode, it would look like that:

File newContent = getSelectedByUser(...);
TreeModel newModel = new MyFileBasedTreeModel(newContent);
//this next part must be done in the EventDispatcherThread
myTree.setModel(newModel); 

then the JTree would be updated, without any call to repaint, etc.

Hope it helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文