• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTree not updating

 
kate damond
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have this JTree in a scroll pane. Each node in the Tree has several attributes, say, for example, Name, Type, Colour, Height. Under the tree, is a combo box. From this, you can select, Type, Colour, Height etc. You select 'Colour' and all the data from the tree is grouped into colours, a new tree is built and .... hey presto, it doesn't appear. The old Tree, grouped by whatever it was grouped by, just sits there.
Here's how I try to show the new tree :

I'm happy with builder.buildTree(), and have tested that seperately, I just can't get the panel to pick up the changes. I tried :

but nothing happened.
What should I try next ?
Thanks,
Kate
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, here is what I had to do. I had my tree in a JScrollPane. I hade to myJPanel.remove(myJScrollPane) and then add it back to the panel again. Then, to actually get the tree to refresh, I tried repaint, but that didn't work. So I had to setVisible(false) and then on the very next line I setVisible(true). That way, it removed the old tree, I called a method that generates a new tree, then added that to the JPanel. Then the panel has to be repainted.
Hope that helps.

Gregg
 
kate damond
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gregg,
thanks for that - you started me off, and eventually, I think I have a concise way to go about it ...
No need to remove the scroll pane, and no need to set the Tree to invisible and back. You just need to set the ScrollPane's ViewPointView, to the new tree. (Actually, you don't even need a new tree, just set the Model for the existing tree - this has the advantage that all existing listeners stay in place) ...


Here, 'newRoot' is a TreeNode returned to me by my builder.buildTree() so it's easy for me to construct just a new Model at that point, rather than a new tree. However, it works if you create your new tree at that point by whatever method, and then do the viewpointView thing
... and the display just updates itself !!!
Hopefully, that helps us both out,
thanks,
Kate
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Kate! I'll give that a shot. Should cut down on some of my overhead.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic