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

mhshams
  • 16,384
  • 17
  • 55
  • 65

2 Answers2

4

TreeWillExpandListener See for example this

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • @StanislavL: the issue is that when i setting the model to Tree, its immediately retrieving all records (almost 2000 record) even before displaying them. – mhshams May 01 '11 at 11:12
  • @StanislavL +1 nice answer I can see how you can use it for this purpose. – Boro May 01 '11 at 11:38
  • @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. – Boro May 01 '11 at 11:49
  • @mohammad shamsi you can fill just the first level of the tree and retrieve children of clicked node before expanding. – StanislavL May 01 '11 at 18:58
  • @Stnislavl the issue is that i have more then 100K items in first level of the tree. – mhshams May 02 '11 at 03:59
  • I think you should change the hierarchy somehow. E.g. add 100 elements and node "more..." clicking on the nodewill load next 100. – StanislavL May 05 '11 at 10:28
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.

Itay Maman
  • 30,277
  • 10
  • 88
  • 118
  • @Maman: i have Custom TreeModel but the result still is the same. immediately after setting the model in JTree, its retrieving all tree nodes. – mhshams May 01 '11 at 11:07
  • How is your tree structured? Do you have a root with 10K immediate children? – Itay Maman May 01 '11 at 11:50
  • @Maman: yeah, almost like that, i have a root with more than 20K immediate children. – mhshams May 01 '11 at 12:58
  • 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. – Itay Maman May 01 '11 at 14:26
  • @Maman with your suggest test, I'm still getting out of memory sometimes (even for first level retrieving) – mhshams May 02 '11 at 04:02
  • Why don't you post your test code? (try to minimize it to the smallest self-contained program that still produces the problem) – Itay Maman May 02 '11 at 09:59