mhshams
mhshams

Reputation: 16972

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

Upvotes: 3

Views: 2108

Answers (2)

Itay Maman
Itay Maman

Reputation: 30733

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.

Upvotes: 1

StanislavL
StanislavL

Reputation: 57421

TreeWillExpandListener See for example this

Upvotes: 4

Related Questions