3

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

2 Answers 2

4

TreeWillExpandListener See for example this

Sign up to request clarification or add additional context in comments.

6 Comments

@StanislavL: the issue is that when i setting the model to Tree, its immediately retrieving all records (almost 2000 record) even before displaying them.
@StanislavL +1 nice answer I can see how you can use it for this purpose.
@mohammad shamsi can you please share the code so I can play with it I hate to start from scratch. And I am interested in a copy-able solution for future reference.
@mohammad shamsi you can fill just the first level of the tree and retrieve children of clicked node before expanding.
@Stnislavl the issue is that i have more then 100K items in first level of the tree.
|
1

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.

6 Comments

@Maman: i have Custom TreeModel but the result still is the same. immediately after setting the model in JTree, its retrieving all tree nodes.
How is your tree structured? Do you have a root with 10K immediate children?
@Maman: yeah, almost like that, i have a root with more than 20K immediate children.
Then you may be exceeding the capabilities of JTree not because of the size of the data but merely due to the number of children. I suggest you hack a custom model that generates 20K children without actually creating objects behind the scenes (use Integer as your objects, and make the Integer 0 to be the root of all Integers from 1..20K) and see how it affects the performance problem.
@Maman with your suggest test, I'm still getting out of memory sometimes (even for first level retrieving)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.