简体   繁体   English

Java Swing JTree TreeModel - 如何做惰性初始化

[英]Java Swing JTree TreeModel - how to do lazy inialization

I have a JTree in my swing app, to display a long list of data (int tree mode).我的 swing 应用程序中有一个 JTree,用于显示一长串数据(int 树模式)。

the issue is that TreeModel loading all items during initialization and I don't need to load them all.问题是 TreeModel 在初始化期间加载了所有项目,我不需要全部加载它们。 in one screen only 100 of them are displayable so no point to load thousands of data to show only 100 of them in one screen.在一个屏幕中只有 100 个是可显示的,因此没有必要加载数千个数据以在一个屏幕中仅显示 100 个。

Question: is there any way to do kind of lazy initialization in TreeModel and retrieve data whenever needed?问题:有什么方法可以在 TreeModel 中进行延迟初始化并在需要时检索数据?

Thanks All谢谢大家

TreeWillExpandListener See for example this TreeWillExpandListener 例如看这个

I am assuming you're using the DefaultTreeModel.我假设您使用的是 DefaultTreeModel。

I had solved such a problem by implementing a custom TreeModel .我通过实现自定义TreeModel解决了这样的问题。 It may seem complicated, but once you get into it you see that it's not that bad.它可能看起来很复杂,但一旦你进入它,你就会发现它并没有那么糟糕。 You have only 8 methods to implement and most of them are quite trivial if you already have a tree-like data structure.你只有 8 种方法可以实现,如果你已经有一个树状数据结构,它们中的大多数都是非常简单的。

The main benefit of this approach is that you get total control over the underlying model.这种方法的主要好处是您可以完全控制底层 model。

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

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