import java.awt.*; import javax.swing.*; import javax.swing.tree.*; import javax.swing.event.*; import java.util.Iterator; import java.util.List; import org.jdom.*; import org.jdom.input.SAXBuilder; import java.io.File; public class XMLTreeViewer extends JFrame { //The JTree to display the XML private JTree xmlTree; //The XML document to be output to the JTree private Document xmlDoc; DefaultMutableTreeNode tn; public XMLTreeViewer(Document doc) { super(); this.xmlDoc = doc; setSize(600, 450); tn = new DefaultMutableTreeNode("Hyper Tree"); initialize(); } private void initialize() { xmlTree = new JTree(); xmlTree.setName("Hyper Tree"); getContentPane().add(new JScrollPane(xmlTree), BorderLayout.CENTER); // getContentPane().add(new JPanel()); processElement(xmlDoc.getRootElement(), tn); ( (DefaultTreeModel) xmlTree.getModel()).setRoot(tn); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { //release all the resource xmlTree = null; tn = null; } }); setVisible(true); //add Listener to Print to Screen the xml tag selected xmlTree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent evt) { // Get all nodes whose selection status has changed TreePath[] paths = evt.getPaths(); // Print the last Path Component selected System.out.println(evt.getPath().getLastPathComponent()); //print the full path from the selected tag // System.out.println(evt.getPath().toString()); } }); } private void processElement(Element el, DefaultMutableTreeNode dmtn) { DefaultMutableTreeNode currentNode = new DefaultMutableTreeNode(el.getName()); String text = el.getTextNormalize(); if ( (text != null) && (!text.equals(""))) { currentNode.add(new DefaultMutableTreeNode(text)); } //processAttributes(el, currentNode); Iterator children = el.getChildren().iterator(); while (children.hasNext()) { processElement( (Element) children.next(), currentNode); } dmtn.add(currentNode); } private void processAttributes(Element el, DefaultMutableTreeNode dmtn) { Iterator atts = el.getAttributes().iterator(); while (atts.hasNext()) { Attribute att = (Attribute) atts.next(); DefaultMutableTreeNode attNode = new DefaultMutableTreeNode("@" + att.getName()); attNode.add(new DefaultMutableTreeNode(att.getValue())); dmtn.add(attNode); } } public static void main(String args[]) throws Exception { SAXBuilder builder = new SAXBuilder(); // Document doc = builder.build(new File("mails.xsd")); Document doc = builder.build(new File("C://Documents and Settings//cs.html")); XMLTreeViewer viewer = new XMLTreeViewer(doc); viewer.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); } }); } }
//add Listener to Print to Screen the xml tag selected
xmlTree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent evt) {
// Get all nodes whose selection status has changed
TreePath[] paths = evt.getPaths();
// Print the last Path Component selected
System.out.println(evt.getPath().getLastPathComponent());
DefaultTreeModel newModel = new DefaultTreeModel((TreeNode)evt.getPath().getLastPathComponent());
xmlTree.setModel(newModel);
//print the full path from the selected tag
// System.out.println(evt.getPath().toString());
}
});Hi,
Thanks for the reply. But by doing the above I will be able to modify the original tree that I have and will not be able to display the original tree that I have.
How can I display the subtree in another window or frame such that my original tree is preserved??
Please help me out in this.....
Thanks!!!
[B][COLOR=#7f0055]import[/COLOR][/B][COLOR=#7f0055][/COLOR][COLOR=#000000] java.awt.*;[/COLOR] [COLOR=#000000][/COLOR] [B][COLOR=#7f0055]import[/COLOR][/B][COLOR=#7f0055][/COLOR][COLOR=#000000] javax.swing.*;[/COLOR] [B][COLOR=#7f0055]import[/COLOR][/B][COLOR=#7f0055][/COLOR][COLOR=#000000] javax.swing.tree.*;[/COLOR] [B][COLOR=#7f0055]import[/COLOR][/B][COLOR=#7f0055][/COLOR][COLOR=#000000] javax.swing.event.*;[/COLOR] [COLOR=#000000][/COLOR] [B][COLOR=#7f0055]import[/COLOR][/B][COLOR=#7f0055][/COLOR][COLOR=#000000] java.util.Iterator;[/COLOR] [B][COLOR=#7f0055]import[/COLOR][/B][COLOR=#7f0055][/COLOR][COLOR=#000000] java.util.List;[/COLOR] [B][COLOR=#7f0055]import[/COLOR][/B][COLOR=#7f0055][/COLOR][COLOR=#000000] org.jdom.*;[/COLOR] [B][COLOR=#7f0055]import[/COLOR][/B][COLOR=#7f0055][/COLOR][COLOR=#000000] org.jdom.input.SAXBuilder;[/COLOR] [B][COLOR=#7f0055]import[/COLOR][/B][COLOR=#7f0055][/COLOR][COLOR=#000000] java.io.File;[/COLOR] [B][COLOR=#7f0055]import[/COLOR][/B][COLOR=#7f0055][/COLOR][COLOR=#000000] java.awt.event.*;[/COLOR] [COLOR=#000000][/COLOR] [B][COLOR=#7f0055]public[/COLOR][/B][COLOR=#7f0055][/COLOR][COLOR=#000000] [/COLOR][B][COLOR=#7f0055]class[/COLOR][/B][COLOR=#7f0055][/COLOR][COLOR=#000000] XMLTreeViewer [/COLOR][B][COLOR=#7f0055]extends[/COLOR][/B][COLOR=#7f0055][/COLOR][COLOR=#000000] JFrame [/COLOR][B][COLOR=#7f0055]implements[/COLOR][/B][COLOR=#7f0055][/COLOR][COLOR=#000000] ActionListener{[/COLOR] [COLOR=#000000][/COLOR] [COLOR=#3f7f5f]//The JTree to display the XML [/COLOR][B][COLOR=#7f0055]private[/COLOR][/B][COLOR=#7f0055][/COLOR] JTree [COLOR=#0000c0]xmlTree[/COLOR]; [B][COLOR=#7f0055]private[/COLOR][/B][COLOR=#7f0055][/COLOR] JTree [COLOR=#0000c0]resultTree[/COLOR]; JButton [COLOR=#0000c0]showButton[/COLOR] ; JButton [COLOR=#0000c0]primeButton[/COLOR]; [COLOR=#3f7f5f]//The XML document to be output to the JTree [/COLOR][B][COLOR=#7f0055]private[/COLOR][/B][COLOR=#7f0055][/COLOR] Document [COLOR=#0000c0]xmlDoc[/COLOR]; DefaultMutableTreeNode [COLOR=#0000c0]tn[/COLOR],[COLOR=#0000c0]selectedNode[/COLOR]; [B][COLOR=#7f0055]public[/COLOR][/B][COLOR=#7f0055][/COLOR] XMLTreeViewer(Document doc) { [B][COLOR=#7f0055]super[/COLOR][/B][COLOR=#7f0055][/COLOR]([COLOR=#2a00ff]"WebOQL Homework"[/COLOR]); [B][COLOR=#7f0055]this[/COLOR][/B][COLOR=#7f0055][/COLOR].[COLOR=#0000c0]xmlDoc[/COLOR] = doc; [COLOR=#3f7f5f]//document [/COLOR]setSize(600, 450); [COLOR=#0000c0]tn[/COLOR] = [B][COLOR=#7f0055]new[/COLOR][/B][COLOR=#7f0055][/COLOR] DefaultMutableTreeNode([COLOR=#2a00ff]"Hyper Tree"[/COLOR]); initialize(); } [B][COLOR=#7f0055]private[/COLOR][/B][COLOR=#7f0055][/COLOR] [B][COLOR=#7f0055]void[/COLOR][/B][COLOR=#7f0055][/COLOR] initialize() { [COLOR=#0000c0]xmlTree[/COLOR] = [B][COLOR=#7f0055]new[/COLOR][/B][COLOR=#7f0055][/COLOR] JTree(); [COLOR=#0000c0]xmlTree[/COLOR].setName([COLOR=#2a00ff]"HyperTree"[/COLOR]); getContentPane().add([B][COLOR=#7f0055]new[/COLOR][/B][COLOR=#7f0055][/COLOR] JScrollPane([COLOR=#0000c0]xmlTree[/COLOR]), BorderLayout.[I][COLOR=#0000c0]LINE_START[/COLOR][/I][COLOR=#0000c0][/COLOR]); getContentPane().add(tabbedPane(),BorderLayout.[I][COLOR=#0000c0]LINE_END[/COLOR][/I][COLOR=#0000c0][/COLOR]); processElement([COLOR=#0000c0]xmlDoc[/COLOR].getRootElement(), [COLOR=#0000c0]tn[/COLOR]); [COLOR=#3f7f5f]//create a tree [/COLOR]( (DefaultTreeModel) [COLOR=#0000c0]xmlTree[/COLOR].getModel()).setRoot([COLOR=#0000c0]tn[/COLOR]); addWindowListener([B][COLOR=#7f0055]new[/COLOR][/B][COLOR=#7f0055][/COLOR] java.awt.event.WindowAdapter() { [B][COLOR=#7f0055]public[/COLOR][/B][COLOR=#7f0055][/COLOR] [B][COLOR=#7f0055]void[/COLOR][/B][COLOR=#7f0055][/COLOR] windowClosing(java.awt.event.WindowEvent e) { [COLOR=#3f7f5f]//release all the resource [/COLOR][COLOR=#0000c0]xmlTree[/COLOR] = [B][COLOR=#7f0055]null[/COLOR][/B][COLOR=#7f0055][/COLOR]; [COLOR=#0000c0]tn[/COLOR] = [B][COLOR=#7f0055]null[/COLOR][/B][COLOR=#7f0055][/COLOR]; } }); setVisible([B][COLOR=#7f0055]true[/COLOR][/B][COLOR=#7f0055][/COLOR]); [COLOR=#3f7f5f]//add Listener to Print to Screen the xml tag selected [/COLOR][COLOR=#0000c0]xmlTree[/COLOR].addTreeSelectionListener([B][COLOR=#7f0055]new[/COLOR][/B][COLOR=#7f0055][/COLOR] TreeSelectionListener() { [B][COLOR=#7f0055]public[/COLOR][/B][COLOR=#7f0055][/COLOR] [B][COLOR=#7f0055]void[/COLOR][/B][COLOR=#7f0055][/COLOR] valueChanged(TreeSelectionEvent evt) { [COLOR=#3f7f5f]// Get all nodes whose selection status has changed [/COLOR]TreePath[] paths = evt.getPaths(); [COLOR=#3f7f5f]// Print the last Path Component selected [/COLOR]System.[I][COLOR=#0000c0]out[/COLOR][/I][COLOR=#0000c0][/COLOR].println([COLOR=#2a00ff]"Path = "[/COLOR] + evt.getPath().getLastPathComponent()); [COLOR=#0000c0]selectedNode[/COLOR] = (DefaultMutableTreeNode) [COLOR=#0000c0]xmlTree[/COLOR].getLastSelectedPathComponent(); } }); } [COLOR=#3f7f5f]//Get Tag and element content [/COLOR][B][COLOR=#7f0055]private[/COLOR][/B][COLOR=#7f0055][/COLOR] [B][COLOR=#7f0055]void[/COLOR][/B][COLOR=#7f0055][/COLOR] processElement(Element el, DefaultMutableTreeNode dmtn) { DefaultMutableTreeNode currentNode = [B][COLOR=#7f0055]new[/COLOR][/B][COLOR=#7f0055][/COLOR] DefaultMutableTreeNode(el.getName()); String text = el.getTextNormalize(); [B][COLOR=#7f0055]if[/COLOR][/B][COLOR=#7f0055][/COLOR] ( (text != [B][COLOR=#7f0055]null[/COLOR][/B][COLOR=#7f0055][/COLOR]) && (!text.equals([COLOR=#2a00ff]""[/COLOR]))) { currentNode.add([B][COLOR=#7f0055]new[/COLOR][/B][COLOR=#7f0055][/COLOR] DefaultMutableTreeNode(text)); } Iterator children = el.getChildren().iterator(); [B][COLOR=#7f0055]while[/COLOR][/B][COLOR=#7f0055][/COLOR] (children.hasNext()) { processElement( (Element) children.next(), currentNode); } dmtn.add(currentNode); } [B][COLOR=#7f0055]private[/COLOR][/B][COLOR=#7f0055][/COLOR] [B][COLOR=#7f0055]void[/COLOR][/B][COLOR=#7f0055][/COLOR] processAttributes(Element el, DefaultMutableTreeNode dmtn) { Iterator atts = el.getAttributes().iterator(); [B][COLOR=#7f0055]while[/COLOR][/B][COLOR=#7f0055][/COLOR] (atts.hasNext()) { Attribute att = (Attribute) atts.next(); DefaultMutableTreeNode attNode = [B][COLOR=#7f0055]new[/COLOR][/B][COLOR=#7f0055][/COLOR] DefaultMutableTreeNode([COLOR=#2a00ff]"@"[/COLOR] + att.getName()); attNode.add([B][COLOR=#7f0055]new[/COLOR][/B][COLOR=#7f0055][/COLOR] DefaultMutableTreeNode(att.getValue())); dmtn.add(attNode); } } [B][COLOR=#7f0055]private[/COLOR][/B][COLOR=#7f0055][/COLOR] JTabbedPane tabbedPane(){ JTabbedPane tp = [B][COLOR=#7f0055]new[/COLOR][/B][COLOR=#7f0055][/COLOR] JTabbedPane (JTabbedPane.[I][COLOR=#0000c0]LEFT[/COLOR][/I][COLOR=#0000c0][/COLOR]); tp.addTab([COLOR=#2a00ff]"Operators"[/COLOR], createPage1()); [B][COLOR=#7f0055]return[/COLOR][/B][COLOR=#7f0055][/COLOR] tp; } [B][COLOR=#7f0055]private[/COLOR][/B][COLOR=#7f0055][/COLOR] JPanel createPage1(){ JPanel pg = [B][COLOR=#7f0055]new[/COLOR][/B][COLOR=#7f0055][/COLOR] JPanel(); Box top = [B][COLOR=#7f0055]new[/COLOR][/B][COLOR=#7f0055][/COLOR] Box(BoxLayout.[I][COLOR=#0000c0]Y_AXIS[/COLOR][/I][COLOR=#0000c0][/COLOR]); [COLOR=#0000c0]primeButton[/COLOR] = [B][COLOR=#7f0055]new[/COLOR][/B][COLOR=#7f0055][/COLOR] JButton([COLOR=#2a00ff]"Prime(')"[/COLOR]); [COLOR=#0000c0]primeButton[/COLOR].addActionListener([B][COLOR=#7f0055]this[/COLOR][/B][COLOR=#7f0055][/COLOR]); top.add([COLOR=#0000c0]primeButton[/COLOR]); pg.add(top); [B][COLOR=#7f0055]return[/COLOR][/B][COLOR=#7f0055][/COLOR] pg; } [B][COLOR=#7f0055]public[/COLOR][/B][COLOR=#7f0055][/COLOR] [B][COLOR=#7f0055]void[/COLOR][/B][COLOR=#7f0055][/COLOR] actionPerformed(ActionEvent e){ DefaultMutableTreeNode tn1; [COLOR=#3f5fbf]/***[/COLOR] [COLOR=#3f5fbf]1.[/COLOR] [COLOR=#3f5fbf]Prime***/ [/COLOR][B][COLOR=#7f0055]if[/COLOR][/B][COLOR=#7f0055][/COLOR](e.getSource().equals([COLOR=#0000c0]showButton[/COLOR])){ System.[I][COLOR=#0000c0]out[/COLOR][/I][COLOR=#0000c0][/COLOR].println([COLOR=#2a00ff]"showButton is clicked."[/COLOR]); tn1 = [B][COLOR=#7f0055]new[/COLOR][/B][COLOR=#7f0055][/COLOR] DefaultMutableTreeNode([COLOR=#2a00ff]"Result: Hyper Tree"[/COLOR]); processPrime([COLOR=#0000c0]xmlDoc[/COLOR].getRootElement(), tn1, 1); [COLOR=#0000c0]resultTree[/COLOR] = [B][COLOR=#7f0055]new[/COLOR][/B][COLOR=#7f0055][/COLOR] JTree(); ( (DefaultTreeModel) [COLOR=#0000c0]resultTree[/COLOR].getModel()).setRoot(tn1); getContentPane().add([B][COLOR=#7f0055]new[/COLOR][/B][COLOR=#7f0055][/COLOR] JScrollPane([COLOR=#0000c0]resultTree[/COLOR]), BorderLayout.[I][COLOR=#0000c0]CENTER[/COLOR][/I][COLOR=#0000c0][/COLOR]); setVisible([B][COLOR=#7f0055]true[/COLOR][/B][COLOR=#7f0055][/COLOR]); } [COLOR=#3f5fbf]/***[/COLOR] [COLOR=#3f5fbf]2.[/COLOR] [COLOR=#3f5fbf]Prime[/COLOR] [COLOR=#3f5fbf]from[/COLOR] [COLOR=#3f5fbf]the[/COLOR] [COLOR=#3f5fbf]tree[/COLOR] [COLOR=#3f5fbf]***/[/COLOR] [B][COLOR=#7f0055]else[/COLOR][/B][COLOR=#7f0055][/COLOR] [B][COLOR=#7f0055]if[/COLOR][/B][COLOR=#7f0055][/COLOR](e.getSource().equals([COLOR=#0000c0]primeButton[/COLOR])){ System.[I][COLOR=#0000c0]out[/COLOR][/I][COLOR=#0000c0][/COLOR].println([COLOR=#2a00ff]"primeButton is clicked."[/COLOR]); tn1 = [B][COLOR=#7f0055]new[/COLOR][/B][COLOR=#7f0055][/COLOR] DefaultMutableTreeNode([COLOR=#2a00ff]"Result: Hyper Tree"[/COLOR]); [COLOR=#3f7f5f]//DefaultMutableTreeNode cloneNode = (DefaultMutableTreeNode) selectedNode.clone(); [/COLOR][B][COLOR=#7f0055]int[/COLOR][/B][COLOR=#7f0055][/COLOR] childCount = [COLOR=#0000c0]selectedNode[/COLOR].getChildCount(); System.[I][COLOR=#0000c0]out[/COLOR][/I][COLOR=#0000c0][/COLOR].println([COLOR=#2a00ff]"loops = "[/COLOR]+ childCount); DefaultMutableTreeNode newChild = [B][COLOR=#7f0055]new[/COLOR][/B][COLOR=#7f0055][/COLOR] DefaultMutableTreeNode(); [B][COLOR=#7f0055]for[/COLOR][/B][COLOR=#7f0055][/COLOR]([B][COLOR=#7f0055]int[/COLOR][/B][COLOR=#7f0055][/COLOR] i = 0; i < childCount; i++){ newChild = (DefaultMutableTreeNode)( [COLOR=#0000c0]selectedNode[/COLOR].getChildAt(0)); tn1.add(newChild); } [COLOR=#0000c0]resultTree[/COLOR] = [B][COLOR=#7f0055]new[/COLOR][/B][COLOR=#7f0055][/COLOR] JTree(); ( (DefaultTreeModel) [COLOR=#0000c0]resultTree[/COLOR].getModel()).setRoot(tn1); getContentPane().add([B][COLOR=#7f0055]new[/COLOR][/B][COLOR=#7f0055][/COLOR] JScrollPane([COLOR=#0000c0]resultTree[/COLOR]), BorderLayout.[I][COLOR=#0000c0]CENTER[/COLOR][/I][COLOR=#0000c0][/COLOR]); setVisible([B][COLOR=#7f0055]true[/COLOR][/B][COLOR=#7f0055][/COLOR]); } } [B][COLOR=#7f0055]private[/COLOR][/B][COLOR=#7f0055][/COLOR] [B][COLOR=#7f0055]void[/COLOR][/B][COLOR=#7f0055][/COLOR] processPrime(Element el, DefaultMutableTreeNode dmtn, [B][COLOR=#7f0055]int[/COLOR][/B][COLOR=#7f0055][/COLOR] lev) { Iterator children = el.getChildren().iterator(); [B][COLOR=#7f0055]while[/COLOR][/B][COLOR=#7f0055][/COLOR] (children.hasNext()) { Object childObject = children.next(); DefaultMutableTreeNode currentNode = [B][COLOR=#7f0055]new[/COLOR][/B][COLOR=#7f0055][/COLOR] DefaultMutableTreeNode(((Element) childObject).getName()); processElement( (Element) childObject, dmtn); } lev--; } [B][COLOR=#7f0055]private[/COLOR][/B][COLOR=#7f0055][/COLOR] [B][COLOR=#7f0055]void[/COLOR][/B][COLOR=#7f0055][/COLOR] processTail(Element el, DefaultMutableTreeNode dmtn, [B][COLOR=#7f0055]int[/COLOR][/B][COLOR=#7f0055][/COLOR] lev) { lev--; } [B][COLOR=#7f0055]public[/COLOR][/B][COLOR=#7f0055][/COLOR] [B][COLOR=#7f0055]static[/COLOR][/B][COLOR=#7f0055][/COLOR] [B][COLOR=#7f0055]void[/COLOR][/B][COLOR=#7f0055][/COLOR] main(String args[]) [B][COLOR=#7f0055]throws[/COLOR][/B][COLOR=#7f0055][/COLOR] Exception { SAXBuilder builder = [B][COLOR=#7f0055]new[/COLOR][/B][COLOR=#7f0055][/COLOR] SAXBuilder(); Document doc = builder.build([B][COLOR=#7f0055]new[/COLOR][/B][COLOR=#7f0055][/COLOR] File([COLOR=#2a00ff]"C://Users//Brugu Maharishi//Documents//Google Talk Received Files//cs3.html"[/COLOR])); XMLTreeViewer viewer = [B][COLOR=#7f0055]new[/COLOR][/B][COLOR=#7f0055][/COLOR] XMLTreeViewer(doc); viewer.addWindowListener([B][COLOR=#7f0055]new[/COLOR][/B][COLOR=#7f0055][/COLOR] java.awt.event.WindowAdapter() { [B][COLOR=#7f0055]public[/COLOR][/B][COLOR=#7f0055][/COLOR] [B][COLOR=#7f0055]void[/COLOR][/B][COLOR=#7f0055][/COLOR] windowClosing(java.awt.event.WindowEvent e) { System.[I]exit[/I](0); } }); } }
import java.awt.*; import javax.swing.*; import javax.swing.tree.*; import javax.swing.event.*; import java.util.Iterator; import java.util.List; import org.jdom.*; import org.jdom.input.SAXBuilder; import java.io.File; import java.awt.event.*; public class XMLTreeViewer extends JFrame implements ActionListener{ //The JTree to display the XML private JTree xmlTree; private JTree resultTree; JButton showButton ; JButton primeButton; //The XML document to be output to the JTree private Document xmlDoc; DefaultMutableTreeNode tn,selectedNode; public XMLTreeViewer(Document doc) { super("WebOQL Homework"); this.xmlDoc = doc; //document setSize(600, 450); tn = new DefaultMutableTreeNode("Hyper Tree"); initialize(); } private void initialize() { xmlTree = new JTree(); xmlTree.setName("HyperTree"); getContentPane().add(new JScrollPane(xmlTree), BorderLayout.LINE_START); getContentPane().add(tabbedPane(),BorderLayout.LINE_END); processElement(xmlDoc.getRootElement(), tn); //create a tree ( (DefaultTreeModel) xmlTree.getModel()).setRoot(tn); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { //release all the resource xmlTree = null; tn = null; } }); setVisible(true); //add Listener to Print to Screen the xml tag selected xmlTree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent evt) { // Get all nodes whose selection status has changed TreePath[] paths = evt.getPaths(); // Print the last Path Component selected System.out.println("Path = " + evt.getPath().getLastPathComponent()); selectedNode = (DefaultMutableTreeNode) xmlTree.getLastSelectedPathComponent(); } }); } //Get Tag and element content private void processElement(Element el, DefaultMutableTreeNode dmtn) { DefaultMutableTreeNode currentNode = new DefaultMutableTreeNode(el.getName()); String text = el.getTextNormalize(); if ( (text != null) && (!text.equals(""))) { currentNode.add(new DefaultMutableTreeNode(text)); } Iterator children = el.getChildren().iterator(); while (children.hasNext()) { processElement( (Element) children.next(), currentNode); } dmtn.add(currentNode); } private void processAttributes(Element el, DefaultMutableTreeNode dmtn) { Iterator atts = el.getAttributes().iterator(); while (atts.hasNext()) { Attribute att = (Attribute) atts.next(); DefaultMutableTreeNode attNode = new DefaultMutableTreeNode("@" + att.getName()); attNode.add(new DefaultMutableTreeNode(att.getValue())); dmtn.add(attNode); } } private JTabbedPane tabbedPane(){ JTabbedPane tp = new JTabbedPane (JTabbedPane.LEFT); tp.addTab("Operators", createPage1()); return tp; } private JPanel createPage1(){ JPanel pg = new JPanel(); Box top = new Box(BoxLayout.Y_AXIS); primeButton = new JButton("Prime(')"); primeButton.addActionListener(this); top.add(primeButton); pg.add(top); return pg; } public void actionPerformed(ActionEvent e){ DefaultMutableTreeNode tn1; /*** 1. Prime***/ if(e.getSource().equals(showButton)){ System.out.println("showButton is clicked."); tn1 = new DefaultMutableTreeNode("Result: Hyper Tree"); processPrime(xmlDoc.getRootElement(), tn1, 1); resultTree = new JTree(); ( (DefaultTreeModel) resultTree.getModel()).setRoot(tn1); getContentPane().add(new JScrollPane(resultTree), BorderLayout.CENTER); setVisible(true); } /*** 2. Prime from the tree ***/ else if(e.getSource().equals(primeButton)){ System.out.println("primeButton is clicked."); tn1 = new DefaultMutableTreeNode("Result: Hyper Tree"); //DefaultMutableTreeNode cloneNode = (DefaultMutableTreeNode) selectedNode.clone(); int childCount = selectedNode.getChildCount(); System.out.println("loops = "+ childCount); DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(); for(int i = 0; i < childCount; i++){ newChild = (DefaultMutableTreeNode)( selectedNode.getChildAt(0)); tn1.add(newChild); } resultTree = new JTree(); ( (DefaultTreeModel) resultTree.getModel()).setRoot(tn1); getContentPane().add(new JScrollPane(resultTree), BorderLayout.CENTER); setVisible(true); } } private void processPrime(Element el, DefaultMutableTreeNode dmtn, int lev) { Iterator children = el.getChildren().iterator(); while (children.hasNext()) { Object childObject = children.next(); DefaultMutableTreeNode currentNode = new DefaultMutableTreeNode(((Element) childObject).getName()); processElement( (Element) childObject, dmtn); } lev--; } private void processTail(Element el, DefaultMutableTreeNode dmtn, int lev) { lev--; } public static void main(String args[]) throws Exception { SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(new File("C://Users//Brugu Maharishi//Documents//Google Talk Received Files//cs3.html")); XMLTreeViewer viewer = new XMLTreeViewer(doc); viewer.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); } }); } }
Sorry......the code given above could not display properly. So I am resending the code back in proper form.
import java.awt.*; import javax.swing.*; import javax.swing.tree.*; import javax.swing.event.*; import java.util.Iterator; import java.util.List; import org.jdom.*; import org.jdom.input.SAXBuilder; import java.io.File; import java.awt.event.*; public class XMLTreeViewer extends JFrame implements ActionListener{ //The JTree to display the XML private JTree xmlTree; private JTree resultTree; JButton showButton ; JButton primeButton; //The XML document to be output to the JTree private Document xmlDoc; DefaultMutableTreeNode tn,selectedNode; public XMLTreeViewer(Document doc) { super("WebOQL Homework"); this.xmlDoc = doc; //document setSize(600, 450); tn = new DefaultMutableTreeNode("Hyper Tree"); initialize(); } private void initialize() { xmlTree = new JTree(); xmlTree.setName("HyperTree"); getContentPane().add(new JScrollPane(xmlTree), BorderLayout.LINE_START); getContentPane().add(tabbedPane(),BorderLayout.LINE_END); processElement(xmlDoc.getRootElement(), tn); //create a tree ( (DefaultTreeModel) xmlTree.getModel()).setRoot(tn); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { //release all the resource xmlTree = null; tn = null; } }); setVisible(true); //add Listener to Print to Screen the xml tag selected xmlTree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent evt) { // Get all nodes whose selection status has changed TreePath[] paths = evt.getPaths(); // Print the last Path Component selected System.out.println("Path = " + evt.getPath().getLastPathComponent()); selectedNode = (DefaultMutableTreeNode) xmlTree.getLastSelectedPathComponent(); } }); } //Get Tag and element content private void processElement(Element el, DefaultMutableTreeNode dmtn) { DefaultMutableTreeNode currentNode = new DefaultMutableTreeNode(el.getName()); String text = el.getTextNormalize(); if ( (text != null) && (!text.equals(""))) { currentNode.add(new DefaultMutableTreeNode(text)); } Iterator children = el.getChildren().iterator(); while (children.hasNext()) { processElement( (Element) children.next(), currentNode); } dmtn.add(currentNode); } private void processAttributes(Element el, DefaultMutableTreeNode dmtn) { Iterator atts = el.getAttributes().iterator(); while (atts.hasNext()) { Attribute att = (Attribute) atts.next(); DefaultMutableTreeNode attNode = new DefaultMutableTreeNode("@" + att.getName()); attNode.add(new DefaultMutableTreeNode(att.getValue())); dmtn.add(attNode); } } private JTabbedPane tabbedPane(){ JTabbedPane tp = new JTabbedPane (JTabbedPane.LEFT); tp.addTab("Operators", createPage1()); return tp; } private JPanel createPage1(){ JPanel pg = new JPanel(); Box top = new Box(BoxLayout.Y_AXIS); primeButton = new JButton("Prime(')"); primeButton.addActionListener(this); top.add(primeButton); pg.add(top); return pg; } public void actionPerformed(ActionEvent e){ DefaultMutableTreeNode tn1; /*** 1. Prime***/ if(e.getSource().equals(showButton)){ System.out.println("showButton is clicked."); tn1 = new DefaultMutableTreeNode("Result: Hyper Tree"); processPrime(xmlDoc.getRootElement(), tn1, 1); resultTree = new JTree(); ( (DefaultTreeModel) resultTree.getModel()).setRoot(tn1); getContentPane().add(new JScrollPane(resultTree), BorderLayout.CENTER); setVisible(true); } /*** 2. Prime from the tree ***/ else if(e.getSource().equals(primeButton)){ // none of this is needed any longer // System.out.println("primeButton is clicked."); // tn1 = new DefaultMutableTreeNode("Result: Hyper Tree"); // //DefaultMutableTreeNode cloneNode = (DefaultMutableTreeNode) selectedNode.clone(); // int childCount = selectedNode.getChildCount(); // System.out.println("loops = "+ childCount); // DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(); // for(int i = 0; i < childCount; i++){ // newChild = (DefaultMutableTreeNode)( selectedNode.getChildAt(0)); // tn1.add(newChild); // } if (resultTree==null){ resultTree = new JTree(); getContentPane().add(new JScrollPane(resultTree), BorderLayout.CENTER); setVisible(true); } resultTree.setModel(new DefaultTreeModel(selectedNode)); } } private void processPrime(Element el, DefaultMutableTreeNode dmtn, int lev) { Iterator children = el.getChildren().iterator(); while (children.hasNext()) { Object childObject = children.next(); DefaultMutableTreeNode currentNode = new DefaultMutableTreeNode(((Element) childObject).getName()); processElement( (Element) childObject, dmtn); } lev--; } private void processTail(Element el, DefaultMutableTreeNode dmtn, int lev) { lev--; } public static void main(String args[]) throws Exception { SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(new File("C://Users//Brugu Maharishi//Documents//Google Talk Received Files//cs3.html")); XMLTreeViewer viewer = new XMLTreeViewer(doc); viewer.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); } }); } }
| DaniWeb Message | |
| Cancel Changes | |