Refreshing JTree

A

Arun

My program starts by choosing an xml file, traversing it etc into a
JTree, adding the JTree to a frame then showing this on the screen.

The traversing is done in an extension of JTree named XMLTree.
The XMLTree is created using for example
JTree buildTree = XMLTree(filename);

That is all and well.

But when i wish to use another file, i load it in again by using the
command
buildTree = XMLTree(filename);

However the tree doesnt refresh. How can i get the tree to refresh with
the new data?
 
B

ByteCoder

Arun said:
My program starts by choosing an xml file, traversing it etc into a
JTree, adding the JTree to a frame then showing this on the screen.

The traversing is done in an extension of JTree named XMLTree.
The XMLTree is created using for example
JTree buildTree = XMLTree(filename);

That is all and well.

But when i wish to use another file, i load it in again by using the
command
buildTree = XMLTree(filename);

However the tree doesnt refresh. How can i get the tree to refresh with
the new data?

Could you post some code from your XMLTree extension?
 
A

Arun

Could you post some code from your XMLTree extension?

Did this before i learnt about treemodels.
I don't want to implement one now!
Heres the code...
Thanks




*************************************************************

import java.util.List;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

import org.jdom.Document;
import org.jdom.Element;


public class XMLTree extends JTree{

public XMLTree(Document doc)
{
super(makeRootNode(doc));
}


private static DefaultMutableTreeNode makeRootNode(Document xmlDom)

{
// Recursively descends the tree and copies corresponding
// dom nodes.
try{
Element rootElement = xmlDom.getRootElement();
DefaultMutableTreeNode rootNode = buildTree(rootElement);
return(rootNode);
}
catch(Exception e) {
// Returns error message if building of tree is not successful
String errorMessage = "Error making root node: " + e;
System.err.println(errorMessage);
e.printStackTrace();
return(new DefaultMutableTreeNode(errorMessage));
}
}

private static DefaultMutableTreeNode buildTree(Element
rootElement)
{
// Makes a JTree node for the root, then makes JTree
// nodes for each child and adds them to the root node.
DefaultMutableTreeNode rootNode = new
DefaultMutableTreeNode(rootElement.getName());
addChildNodes(rootNode,rootElement);
return(rootNode);
}

private static void addChildNodes(DefaultMutableTreeNode
parentNode, Element parentElement)
{
// Creates list of all children of current parent element
List allChildren = parentElement.getChildren();
// Checks to see the element has any children
if (allChildren.size() != 0)
{
for( int x = 0; x < allChildren.size(); x++)
{
// Creates a new element from current element in list
Element childElement = (Element)allChildren.get(x);
// Converts this element into a node
DefaultMutableTreeNode childNode = new
DefaultMutableTreeNode(childElement.getName());
// Adds this node to the current parent node
parentNode.add(childNode);
// Checks to see if this child node has a child node of
itself.
// If so, then adds child node to that. Recursive procedure.
addChildNodes(childNode, childElement);
}
}
}




}
 
A

Arun

Would it be possible to make a new TreeModel from a file and when that's
done apply it to the JTree?

I'm not sure if that's useful, but here it is anyway:
Good luck! :)

--


I've already read that tutorial.
Would it be possible for you to give me more detail on what you said
before that please?
 
B

ByteCoder

Arun said:
Would it be possible to make a new TreeModel from a file and when
that's

done apply it to the JTree?

I'm not sure if that's useful, but here it is anyway:

<http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html>
[...]
I've already read that tutorial.
Would it be possible for you to give me more detail on what you said
before that please?

Unfortunately not. The TreeModel interface only has get methods. I
didn't see any set methods.

I hoped it would work like the DefaultListModel and the JList, but
unfortunately, it doesn't. :(
 
M

Michael Wong

You *should* be using a TreeModel, but if you want to use the code you have,
you should be removing the old tree from the frame, then adding the new one
in it's place. Remember that you're reassigning the variable "buildTree",
but the frame still has a reference to the old one, which it will continue
to display until you tell it otherwise...

class Foo {
JFrame myFrame;
JTree buildTree;

void loadTree(String filename) {
myFrame.getContentPane().remove(buildTree);
buildTree = new XMLTree(filename);
myFrame.getContentPane().add(buildTree);
myFrame.validate();
}
}
 

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

Similar Threads

DOM extension of JTree 2
TreeModel 2
JTree navigation 1
JTable within JTree 1
JTree Directory System 3
JTree Directory System 2
JTree, setSelectionPath 0
First steps in setting up VSCode to work with Python. 2

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top