简体   繁体   English

Java-更新JTree

[英]Java- updating JTree

I've got a JTree which I'm using to display some (unsurprisingly) hierarchical data. 我有一个JTree,我用它来显示一些(不出所料)的分层数据。 Part of the spec is that the user can change the data source (atm it's just between files). 规范的一部分是用户可以更改数据源(仅仅是在文件之间)。 Now, when this happens, I can rebuild the data and the tree nodes with no problem. 现在,当发生这种情况时,我可以毫无问题地重建数据和树节点。 But, I'm having substantial difficulties getting the tree to update the changes. 但是,我在让树更新更改时遇到很大困难。 I tried removing it from it's scrollpane and replacing with a new JTree, but I didn't see any such. 我尝试从它的滚动窗格中删除它并替换为新的JTree,但我没有看到任何这样的。 I've tried removing all from the JTree and didn't see any effect. 我试过从JTree中删除所有内容并没有看到任何影响。

How can I make the JTree display changes after it's been constructed? 如何在构建之后使JTree显示更改?
Ninjedit: Yes, I did call updateUI(). Ninjedit:是的,我确实调用了updateUI()。

Another edit: I also wanted to replace the tree's current data with my new data. 另一个编辑:我还想用我的新数据替换树的当前数据。 However, I don't see any methods that will take the DefaultMutableTreeNode that I constructed with. 但是,我没有看到任何方法将采用我构造的DefaultMutableTreeNode。 Even if I just remove the JTree and call updateUI on it's containing ScrollPane, nothing happens. 即使我只删除了JTree并在其上调用了包含ScrollPane的updateUI,也没有任何反应。 Or if I use repaint instead. 或者,如果我使用重绘。

It could be that the proper events (the JTree internal events) are not being fired. 可能是正确的事件(JTree内部事件)没有被触发。 For example, you can add nodes either by using node.add(...) or even better, model.insertNodeInto(...) (assuming you're using the DefaultTreeModel ). 例如,您可以使用node.add(...)或甚至更好的model.insertNodeInto(...)添加节点(假设您使用的是DefaultTreeModel )。 In this case, the latter method is preferred as it will fire appropriate events that will cause the view (the JTree) to update correctly. 在这种情况下,后一种方法是首选方法,因为它将触发将导致视图(JTree)正确更新的适当事件。 It's possible that your problem isn't with redrawing the UI, but in fact notifying the view that the model has changed. 您的问题可能不是重绘UI,而是实际上通知视图模型已更改。
So, I would suggest looking in to how you're dynamically modifying your JTree, and if possible I'd suggest using the DefaultTreeModel as your model to drive the view. 因此,我建议您查看如何动态修改JTree,如果可能,我建议使用DefaultTreeModel作为模型来驱动视图。

And just to make sure, you've read through the Sun JTree tutorials , right? 只是为了确保,您已经阅读了Sun JTree教程 ,对吗?

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

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