Click here to Skip to main content
15,895,815 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a tree model which preview XML file in Jtree

but it writes the name of nodes and NULL value beside each node

I want only the name of node appears.
this is my code:

public class XmlTreeDemo extends JFrame {
XmlTreeDemo(String title){
super(title);
try{
DocumentBuilderFactory IDocumentBuilderFactory
= DocumentBuilderFactory.newInstance();
DocumentBuilder IDocumentBuilder
= IDocumentBuilderFactory.newDocumentBuilder();
Document IDocument = IDocumentBuilder.parse("SRS_instance.xml");
Node root = IDocument.getDocumentElement();
XmlTreeModel model = new XmlTreeModel(root);
JTree IJTree = new JTree();
IJTree.setModel(model);
getContentPane().add(new JScrollPane(IJTree),BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
catch (Exception e){
System.err.println(e);
}
}
public static void main(String[] args){
XmlTreeDemo IJTreeDemo = new XmlTreeDemo("Xml tree demo");
IJTreeDemo.pack();
IJTreeDemo.setVisible(true);
}
}
class XmlTreeModel implements TreeModel{
protected Node root;
public XmlTreeModel(Node root){
this.root = root;
}
public Object getRoot(){
return (Object)this.root;
}
public boolean isLeaf(Object node){
if ((((Node)node).getNodeType() == 7) || (((Node)node).getNodeType()
== 1)) return false;
return true;
}
public int getChildCount(Object parent){
return ((Node)parent).getChildNodes().getLength();
}
public Object getChild(Object parent,int index){
Node child = ((Node)parent).getChildNodes().item(index);
return (Object)child;
}
public int getIndexOfChild(Object parent, Object child){
NodeList childs = ((Node)parent).getChildNodes();
if (childs.getLength() == 0) return -1;
for (int i=0; i<childs.getLength(); i++){
if (childs.item(i) == (Node)child) return i;
}
return -1;
}
public void valueForPathChanged(TreePath path, Object newValue){
}
public void addTreeModelListener(TreeModelListener l){
}
public void removeTreeModelListener(TreeModelListener l){
}

plz Help =(
Posted

1 solution

The 'null' value most likely appears besides the text you want to see because, by default (at least from what I know), the JTree's tree cell renderer gets the value to be printed/shown from the object's 'toString()' method.

You could notice this by a simple 'println' after you get the root of the document:
Node root = IDocument.getDocumentElement();
System.out.println("Root node displayed value: "+root.toString());


And, thus, the null value should appear.

An obvious solution would be to override the object's toString() method, but that's not always quite convenient, especially since you are using a class not made by you. ( 'Node' )

So, another option you might go for might be to override the JTree's TreeCellRenderer by building a class that implements the interface TreeCellRenderer and then attach an instance of it to the JTree object you are using with the setCellRenderer() method.

For example:

public class CustomTreeCellRenderer implements TreeCellRenderer {
	public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {

		Component returnValue = null;

		// if the value is a valid object for our purposes
		if (value != null && value instanceof Node) {
			// for no real reason, I decided to use a JLabel, but any class that extends Component will do
			JLabel thisLabel;
			
			// if the node is a leaf, print the value
			if (((Node) value).getNodeType() != 7 && ((Node) value).getNodeType() != 1)
				thisLabel = new JLabel(((Node) value).getNodeValue());
			// else, print the name
			else
				thisLabel = new JLabel(((Node) value).getNodeName());
			
			// make the label the return value
			returnValue = thisLabel;
		} else {
			// else, do what the JTree would have done with the default TreeCellRenderer
			DefaultTreeCellRenderer thisDefaultRenderer = new DefaultTreeCellRenderer();
			returnValue = thisDefaultRenderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
		}

		// return the value
		return returnValue;
	}
}


Then you can use this class like so:
// create the renderer
CustomTreeCellRenderer newRenderer = new CustomTreeCellRenderer();

// what you did earlier
JTree IJTree = new JTree();
IJTree.setModel(model);

// attach it to the JTree
IJTree.setCellRenderer(newRenderer);


Of course, this is just an example. You can tailor the end class to your needs.

I hope this helps.
Cheers!
 
Share this answer
 
Comments
Hackaholic 27-Mar-11 23:39pm    
it WORKS now!!!!
thank you very much ... Cheeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeers! =)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900