• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Redraw JTree in Swing

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. I'm somewhat new to Java and Swing. I'm working on a relatively simple demo application that builds and saves a JTree to file. Everything works great, but I have a question about the best way to redraw the JTree.

My screen is structurued as follows:
JTree inside a JScrollPane inside a JSplitPane inside a JFrame.

When I open my saved file, I re-create the JTree based on the data from the file, and I need to re-paint the JTree, etc. In looking at other posts here, it is suggested that I use the revalidate() command and repaint() command. I've tried various combinations without success.

The only way I've gotten this to work is as follows:

frame.remove(splitPane);
makeSplitPane(); // re-build the entire splitpane
frame.setContentPane(splitPane);
frame.setVisible(true);

This works fine, but it doesn't seem like the best way to do this. I'm probably missing something simple. Any suggestions will be appreciated. Thanks. Mark Dexter
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Dexter:
When I open my saved file, I re-create the JTree based on the data from the file, and I need to re-paint the JTree, etc.



You don't give us a lot to work with, but how are you reading data from your file and putting it in your TreeModel?

One approach is something like this:

TreeModel newTreeModel = createNewModelFromInputFileSomehow("filename.txt");
myTree.setModel(newTreeModel); // replace the tree's current model

That should repaint the tree for you automatically, without having to revalidate() or repaint().
 
Mark Dexter
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help. I'm following the Java Tutorials example http://java.sun.com/docs/books/tutorial/uiswing/examples/components/TreeDemoProject/src/components/TreeDemo.java
and it doesn't use a TreeModel.

I'll try re-doing it with a TreeModel and see if that fixes the problem. The basic idea is that just replacing the TreeModel should automatically cause the JTree to be re-drawn. Is that correct? Thanks again. Mark Dexter
 
Brian Cole
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's an official Sun demo? I don't like how the nested BookInfo class is not declared to be static.

Anyway, you are sort of using TreeModels already, you just don't know it. The line that does
tree = new JTree(top);
is the same as doing
DefaultTreeModel myTreeModel = new DefaultTreeModel(top);
tree = new JTree(myTreeModel);


So in your case you could do something like
DefaultMutableTreeNode newTop = new DefaultMutableTreeNode("new root");
createDifferentNodes(newTop); // a la the example's createNodes() method
tree.setModel(new DefaultTreeModel(newTop));


The other thing you could do to have the tree automatically update is to change the node structure using the DefaultTreeModel methods (instead of using the DefaultMutableTreeNode methods). For example:
myTreeModel.insertNodeInto(newChildNode, parentNode, index);
myTreeModel.removeNodeFromParent(childNode);
myTreeModel.setRoot(newRootNode);
 
Mark Dexter
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The other thing you could do to have the tree automatically update is to change the node structure using the DefaultTreeModel methods (instead of using the DefaultMutableTreeNode methods). For example:
myTreeModel.insertNodeInto(newChildNode, parentNode, index);
myTreeModel.removeNodeFromParent(childNode);
myTreeModel.setRoot(newRootNode);



Brian, thanks for your help. Your second idea (above) worked. I couldn't get this working without using the TreeModel. Also, in case this helps someone in the future, it doesn't update the display correctly if you re-create the TreeModel (which is what I was doing!). You can re-create the root, but only if you do the setRoot command.

Thanks again. Mark Dexter
 
reply
    Bookmark Topic Watch Topic
  • New Topic