By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,779 Members | 1,078 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,779 IT Pros & Developers. It's quick & easy.

Display xml file in JTree: Select information to be shown in node label

P: n/a
Hi there,
I have managed to write a small java program that display an xml file
in a JTree. This was achieved by defining a suitable tree model. THe
program works...somehow. The nodes (elements and texts) are correctly
recognized, and the corresponding branches in the JTree can be opened
and closed. However, when I run the program, all the non-leaf nodes
(e. g. the element nodes) are displayed from the start tag to the end
tag including the tag delimiters. Is there any way to control what is
used as a "node label" in a JTree? Of course that could be done by
brute (e.g. by building the JTree from simple strings) but I believe
that this approach will destroy the tree structure of the underlying
xml file.
I have tried for some time to display xml data in a tree view, and a
java solution seemed to be the most "organic" one. So it would be
great if that program to be really made to work properly. Many thanks
for any idea how to achieve this! Piet
Here is the code:
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;

public class XmlTreeDemo extends JFrame {
XmlTreeDemo(String title){
super(title);
try{
DocumentBuilderFactory IDocumentBuilderFactory
= DocumentBuilderFactory.newInstance();
DocumentBuilder IDocumentBuilder
= IDocumentBuilderFactory.newDocumentBuilder();
Document IDocument = IDocumentBuilder.parse("c:/test1.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.show();
}
}
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){
}
}
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Piet wrote:
Hi there,
I have managed to write a small java program that display an xml file
in a JTree. This was achieved by defining a suitable tree model. THe
program works...somehow. The nodes (elements and texts) are correctly
recognized, and the corresponding branches in the JTree can be opened
and closed. However, when I run the program, all the non-leaf nodes
(e. g. the element nodes) are displayed from the start tag to the end
tag including the tag delimiters. Is there any way to control what is
used as a "node label" in a JTree? Of course that could be done by
brute (e.g. by building the JTree from simple strings) but I believe
that this approach will destroy the tree structure of the underlying
xml file.


The default rendering will call toString() on the object associated to
the node. So, if appropriate, you could override toString() to get what
you want.

Of course, that is the easy way and is frequently not possible. In such
a case, you can implement and set a custom TreeCellRenderer to display
whatever you like.

Ray

--
XML is the programmer's duct tape.
Jul 17 '05 #2

P: n/a
Raymond DeCampo <rd******@spam.twcny.spam.rr.spam.com.spam> wrote in message news:<kK*******************@twister.nyroc.rr.com>. ..
Hi Raymond,
many thanks for the hint! In fact it was only 10 lines of code to improve it.
Piet
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.