Display the jTree

G

groupie

Hi,
I have a jTree control created via the netbeans graphical gui. It's
displaying the default 'colours', 'sports', and 'food'. The tree is
in
a jPanel which is initially invisible. After creating the tree I make
everything visible:

jTree1 = new names().buildTree();
jPanel1.setVisible(true);


names().buildTree() is returning a full jTree structure into the tree
gui (jTree1) , however when the panel is displayed, the tree is still
displaying the default. I tried jTree1.repaint() but that didn't
work.


Any ideas? Do I have to refesh the screen or something?


Thanks.
 
J

John

Sounds like you need to reload the tree after rebuilding it:

((DefaultTreeModel) jTree1.getModel()).reload();
 
G

groupie

Sounds like you need to reload the tree after rebuilding it:

((DefaultTreeModel) jTree1.getModel()).reload();

Thanks for that, but it didn't seem to work unfortunatly. Still
display sthe default tree.
 
M

Mark Space

groupie said:
Thanks for that, but it didn't seem to work unfortunatly. Still
display sthe default tree.

Where do you do the init of the JTree? I had the same problem.
Basically, I think the correct solution is to initialize the JTree
before pack() and setVisible() is called on the higher level container
(JFrame, JWindow, etc.)

On the left, in the Inspector window, select the JTree component then
right click and open the properties window. Select the CODE tab. You
can add code here. I added two lines in the pre-creation code, and one
line in the custom creation code line. I use the minimum number of
lines because it's a pretty small box you have to enter it in, and I can
call a static method with one line, and put everything important in
there where it's easy to modify. (As opposed to the little box in the
properties dialog, which is not easy to work with at all.)

Pre-creation:
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Scenario");
createSideMenu(top);

Custom creation code:
new javax.swing.JTree(top);

Now I can just edit the "createSideMenu()" method if I need to change
anything.

I think there's a way to update the JFrame when one of it's components
is modified, but I haven't investigated it yet. The code above goes
around the problem by making the tree correctly right from the beginning.

Good luck.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top