Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Redraw JTree in Swing

 
Mark Dexter
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • 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
 
Brian Cole
Author
Ranch Hand
Posts: 924
1
  • Mark post as helpful
  • send pies
  • 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
  • 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
Ranch Hand
Posts: 924
1
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
No more fooling around. Read this tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!