JTree实时更新?

发布于 2024-08-04 21:07:35 字数 207 浏览 4 评论 0原文

我正在寻找一种线程解决方案来每秒更新我的 JTree。

基本上,我正在...

  • 导入外部文件
  • 创建枚举 从枚举
  • 构建树

外部文件可以随机更改,树中的数据需要以某种合理的时间方式表示此更改。我怎样才能在不重新绘制 JTree(通过面板)所在的整个内容窗格的情况下重绘树?

谢谢!

I'm looking for a threaded solution to updating my JTree every second.

Basically, I'm...

  • Importing an external file
  • Creating an Enumeration off of that
  • Building the tree off the enumeration

The external file can change at random, and the data in the tree needs to represent this change in a somewhat reasonable time manner. How would I be able to have the tree redraw without repainting the entire content pane which the JTree (via a panel) is in?

Thanks!

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

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

发布评论

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

评论(5

很酷又爱笑 2024-08-11 21:07:35

只需让 JTree 的模型触发更新,JTree 就会重新绘制自身。最简单的方法是使用 DefaultTreeModel,当文件更改时,从文件中构造 TreeNodes(可能使用 DefaultMutableTreeNode),并使用新创建的节点树的基本节点在模型上调用 setRoot()。

Just have the JTree's model fire an update, and the JTree will repaint itself. The easiest way to do this is to use a DefaultTreeModel, and when the file changes, construct TreeNodes (possibly using DefaultMutableTreeNode) from your file, and call setRoot() on the model with the base node of your newly created node tree.

你爱我像她 2024-08-11 21:07:35

我想您可能在其他地方读过这篇文章:Swing 不是线程保存的;)。如果您无论如何都想从不同的线程更新 Swing 组件,则必须让其他线程将其更新推送到 Swing 的事件线程上。执行此操作的方法是 EventQueue.invokeAndWait(Runnable)。

下面是类似问题的代码示例(使 JTree 与目录树同步):
http://www.onyxbits.de/ content/java-and-directory-trees-joy-implementing-simple-filemanager

您应该对 FileMonitor.java 文件感兴趣。

I guess you probably read this somewhere else: Swing is not thread save ;). If you want to update a Swing component from a different thread anyway, you have to make that other thread push it's updates on Swing's event thread. The method to do this is EventQueue.invokeAndWait(Runnable).

Here's a code example for a similar problem (making a JTree sync with a directory tree):
http://www.onyxbits.de/content/java-and-directory-trees-joy-implementing-simple-filemanager

You should be interested in the FileMonitor.java file.

南笙 2024-08-11 21:07:35

如果您的树使用 DefaultTreeModel,请尝试此操作

((DefaultTreeModel)yourTree.getModel()).reload();

Try this if your tree uses the DefalultTreeModel

((DefaultTreeModel)yourTree.getModel()).reload();
少女七分熟 2024-08-11 21:07:35

如果重新绘制 JTree 组件,绘制区域将被剪掉包含面板的其余部分。

If you repaint the JTree component, the paint region will be clip out the rest of the containing panel.

森末i 2024-08-11 21:07:35

如果你有很多节点,我会做@Tom 建议的事情。 @CarlG 的解决方案可能适用于少数节点。如果更新整个树模型,每一秒都会遇到性能问题。

如果需要的话,我只会更新树的可见边界,并在某处保留一些 AST(语法树)。如果树模型(来自解析的文档)每秒都在变化,我严重怀疑用户是否有时间每秒滚动所有树。

User scrolled to bounds XXX
A = First visible node
B = Last visible node
if someRangeBefore(A) is dirty update
If region(A, B) is dirty
  update nodes

实施有效且非常有效的节点更新策略会比上面更复杂。

I would do something like @Tom suggested if you have lots of nodes. The solution of @CarlG is probably fine for few nodes. If you update the entire tree model, every second you run into performance issues.

I would update only the visible bounds of the tree, if needed, and keep some AST(Syntax tree) somewhere. If the tree model(from the parsed document) is changing every second, I seriously doubt that the user will have time to scroll all the tree each second.

User scrolled to bounds XXX
A = First visible node
B = Last visible node
if someRangeBefore(A) is dirty update
If region(A, B) is dirty
  update nodes

It would be more complicated than above to implement a working and very efficient strategy to update nodes.

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