Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

the issue is that TreeModel loading all items during initialization and I don't need to load them all. 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.

Question: is there any way to do kind of lazy initialization in TreeModel and retrieve data whenever needed ?

Thanks All

like image 720
mhshams Avatar asked Jan 28 '26 16:01

mhshams


2 Answers

TreeWillExpandListener See for example this

like image 88
StanislavL Avatar answered Jan 30 '26 05:01

StanislavL


I am assuming you're using the DefaultTreeModel.

I had solved such a problem by implementing a custom 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.

The main benefit of this approach is that you get total control over the underlying model.

like image 21
Itay Maman Avatar answered Jan 30 '26 06:01

Itay Maman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!