User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 456,554 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,485 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 929 | Replies: 10
Reply
Join Date: Jun 2007
Posts: 10
Reputation: flavour_of_bru is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
flavour_of_bru flavour_of_bru is offline Offline
Newbie Poster

Help needed with JTree

  #1  
Oct 16th, 2007
Hi,

I am converting a given html document into a tree structure and displaying it using JTree and DefaultMutable classes.

By using the html parser and the JTree functionality, I can display the html doc as tree structure, but now I would like to get the subtrees only of that tree when clicked on any of the nodes in the tree or else If I input the node, I need to display only the subtree associated with that node and not the entire tree. Is there any function to do that or else if I need to parse with along the tree structure, How can I do that and what functions and classes are available for that.

Any help is appreciated.

Thanks!!!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,090
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 307
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Sensei

Re: Help needed with JTree

  #2  
Oct 16th, 2007
I'm not clear on what you want to do with the subtrees when you say "get the subtrees". If you select a node, you essentially have that subtree via it's children collection. What you do with that collection depends on your needs, so perhaps a bit more info would help.
Reply With Quote  
Join Date: Jun 2007
Posts: 10
Reputation: flavour_of_bru is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
flavour_of_bru flavour_of_bru is offline Offline
Newbie Poster

Re: Help needed with JTree

  #3  
Oct 16th, 2007
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import javax.swing.tree.*;
  4. import javax.swing.event.*;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import org.jdom.*;
  8. import org.jdom.input.SAXBuilder;
  9. import java.io.File;
  10.  
  11. public class XMLTreeViewer extends JFrame {
  12.  
  13. //The JTree to display the XML
  14. private JTree xmlTree;
  15.  
  16. //The XML document to be output to the JTree
  17. private Document xmlDoc;
  18. DefaultMutableTreeNode tn;
  19.  
  20. public XMLTreeViewer(Document doc) {
  21. super();
  22. this.xmlDoc = doc;
  23. setSize(600, 450);
  24. tn = new DefaultMutableTreeNode("Hyper Tree");
  25. initialize();
  26. }
  27.  
  28. private void initialize() {
  29. xmlTree = new JTree();
  30. xmlTree.setName("Hyper Tree");
  31. getContentPane().add(new JScrollPane(xmlTree), BorderLayout.CENTER);
  32. // getContentPane().add(new JPanel());
  33. processElement(xmlDoc.getRootElement(), tn);
  34. ( (DefaultTreeModel) xmlTree.getModel()).setRoot(tn);
  35. addWindowListener(new java.awt.event.WindowAdapter() {
  36. public void windowClosing(java.awt.event.WindowEvent e) {
  37. //release all the resource
  38. xmlTree = null;
  39. tn = null;
  40. }
  41. });
  42. setVisible(true);
  43. //add Listener to Print to Screen the xml tag selected
  44. xmlTree.addTreeSelectionListener(new TreeSelectionListener() {
  45. public void valueChanged(TreeSelectionEvent evt) {
  46. // Get all nodes whose selection status has changed
  47. TreePath[] paths = evt.getPaths();
  48. // Print the last Path Component selected
  49. System.out.println(evt.getPath().getLastPathComponent());
  50.  
  51. //print the full path from the selected tag
  52. // System.out.println(evt.getPath().toString());
  53. }
  54. });
  55. }
  56.  
  57. private void processElement(Element el, DefaultMutableTreeNode dmtn) {
  58. DefaultMutableTreeNode currentNode = new DefaultMutableTreeNode(el.getName());
  59. String text = el.getTextNormalize();
  60. if ( (text != null) && (!text.equals(""))) {
  61. currentNode.add(new DefaultMutableTreeNode(text));
  62. }
  63. //processAttributes(el, currentNode);
  64.  
  65. Iterator children = el.getChildren().iterator();
  66. while (children.hasNext()) {
  67. processElement( (Element) children.next(), currentNode);
  68. }
  69. dmtn.add(currentNode);
  70. }
  71.  
  72. private void processAttributes(Element el, DefaultMutableTreeNode dmtn) {
  73. Iterator atts = el.getAttributes().iterator();
  74.  
  75. while (atts.hasNext()) {
  76. Attribute att = (Attribute) atts.next();
  77. DefaultMutableTreeNode attNode = new DefaultMutableTreeNode("@" + att.getName());
  78. attNode.add(new DefaultMutableTreeNode(att.getValue()));
  79. dmtn.add(attNode);
  80. }
  81. }
  82.  
  83. public static void main(String args[]) throws Exception {
  84. SAXBuilder builder = new SAXBuilder();
  85. // Document doc = builder.build(new File("mails.xsd"));
  86. Document doc = builder.build(new File("C://Documents and Settings//cs.html"));
  87. XMLTreeViewer viewer = new XMLTreeViewer(doc);
  88. viewer.addWindowListener(new java.awt.event.WindowAdapter() {
  89. public void windowClosing(java.awt.event.WindowEvent e) {
  90. System.exit(0);
  91. }
  92. });
  93. }
  94. }

By using the above code, I am able to read an html document and display the resultant as a tree. Now I would like to define some operators on it.
In the tree structure, I would like to display only subtrees that I want. For example, if the root node is "html" and it has 2 children "body" and "head" then if I select the node "body" then i want to display only the subtree associated with the node "body" and so on.

So I would like to know if there are any predefined functions for tree traversal or if any other links that might help me out to get the desired result.

Thanks!!
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,090
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 307
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Sensei

Re: Help needed with JTree

  #4  
Oct 17th, 2007
I'm still not exactly clear on the behavior you are wanting. You wish to replace the displayed tree with the selected subtree? Or you want to present that subtree in another location separate from the main document tree?

If you want to replace the existing displayed tree with a selected subtree, you can change your selection listenter to do this
    //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());
      }
    });
That of course discards the overall document model unless you retain that as a separate reference, which you would need if you wanted any way to go back after navigation.

I'm not sure if that is what you are wanting, but maybe it gets you close. If it totally misses what you are wanting to do, post a little more detail on what you wish to do with the selected subtree.
Last edited by Ezzaral : Oct 17th, 2007 at 1:10 pm. Reason: typo
Reply With Quote  
Join Date: Jun 2007
Posts: 10
Reputation: flavour_of_bru is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
flavour_of_bru flavour_of_bru is offline Offline
Newbie Poster

Re: Help needed with JTree

  #5  
Oct 17th, 2007
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!!!
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,090
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 307
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Sensei

Re: Help needed with JTree

  #6  
Oct 17th, 2007
Originally Posted by flavour_of_bru View Post
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!!!

Just create a new JTree component and use the code above to create it's model. In my example, I replaced the existing model the new one. You can just as easily keep the original and work with multiple JTrees.
Reply With Quote  
Join Date: Jun 2007
Posts: 10
Reputation: flavour_of_bru is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
flavour_of_bru flavour_of_bru is offline Offline
Newbie Poster

Re: Help needed with JTree

  #7  
Oct 18th, 2007
  1. [b][color=#7f0055]import[/color][/b][color=#7f0055][/color][color=#000000] java.awt.*;[/color]
  2. [color=#000000][/color]
  3. [b][color=#7f0055]import[/color][/b][color=#7f0055][/color][color=#000000] javax.swing.*;[/color]
  4. [b][color=#7f0055]import[/color][/b][color=#7f0055][/color][color=#000000] javax.swing.tree.*;[/color]
  5. [b][color=#7f0055]import[/color][/b][color=#7f0055][/color][color=#000000] javax.swing.event.*;[/color]
  6. [color=#000000][/color]
  7. [b][color=#7f0055]import[/color][/b][color=#7f0055][/color][color=#000000] java.util.Iterator;[/color]
  8. [b][color=#7f0055]import[/color][/b][color=#7f0055][/color][color=#000000] java.util.List;[/color]
  9. [b][color=#7f0055]import[/color][/b][color=#7f0055][/color][color=#000000] org.jdom.*;[/color]
  10. [b][color=#7f0055]import[/color][/b][color=#7f0055][/color][color=#000000] org.jdom.input.SAXBuilder;[/color]
  11. [b][color=#7f0055]import[/color][/b][color=#7f0055][/color][color=#000000] java.io.File;[/color]
  12. [b][color=#7f0055]import[/color][/b][color=#7f0055][/color][color=#000000] java.awt.event.*;[/color]
  13. [color=#000000][/color]
  14. [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]
  15. [color=#000000][/color]
  16. [color=#3f7f5f]//The JTree to display the XML
  17. [/color][b][color=#7f0055]private[/color][/b][color=#7f0055][/color] JTree [color=#0000c0]xmlTree[/color];
  18. [b][color=#7f0055]private[/color][/b][color=#7f0055][/color] JTree [color=#0000c0]resultTree[/color];
  19. JButton [color=#0000c0]showButton[/color] ;
  20. JButton [color=#0000c0]primeButton[/color];
  21.  
  22. [color=#3f7f5f]//The XML document to be output to the JTree
  23. [/color][b][color=#7f0055]private[/color][/b][color=#7f0055][/color] Document [color=#0000c0]xmlDoc[/color];
  24. DefaultMutableTreeNode [color=#0000c0]tn[/color],[color=#0000c0]selectedNode[/color];
  25.  
  26. [b][color=#7f0055]public[/color][/b][color=#7f0055][/color] XMLTreeViewer(Document doc) {
  27. [b][color=#7f0055]super[/color][/b][color=#7f0055][/color]([color=#2a00ff]"WebOQL Homework"[/color]);
  28. [b][color=#7f0055]this[/color][/b][color=#7f0055][/color].[color=#0000c0]xmlDoc[/color] = doc; [color=#3f7f5f]//document
  29. [/color]setSize(600, 450);
  30. [color=#0000c0]tn[/color] = [b][color=#7f0055]new[/color][/b][color=#7f0055][/color] DefaultMutableTreeNode([color=#2a00ff]"Hyper Tree"[/color]);
  31. initialize();
  32. }
  33.  
  34. [b][color=#7f0055]private[/color][/b][color=#7f0055][/color] [b][color=#7f0055]void[/color][/b][color=#7f0055][/color] initialize() {
  35. [color=#0000c0]xmlTree[/color] = [b][color=#7f0055]new[/color][/b][color=#7f0055][/color] JTree();
  36. [color=#0000c0]xmlTree[/color].setName([color=#2a00ff]"HyperTree"[/color]);
  37. 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]);
  38. getContentPane().add(tabbedPane(),BorderLayout.[i][color=#0000c0]LINE_END[/color][/i][color=#0000c0][/color]);
  39. processElement([color=#0000c0]xmlDoc[/color].getRootElement(), [color=#0000c0]tn[/color]); [color=#3f7f5f]//create a tree
  40. [/color]( (DefaultTreeModel) [color=#0000c0]xmlTree[/color].getModel()).setRoot([color=#0000c0]tn[/color]);
  41. addWindowListener([b][color=#7f0055]new[/color][/b][color=#7f0055][/color] java.awt.event.WindowAdapter() {
  42. [b][color=#7f0055]public[/color][/b][color=#7f0055][/color] [b][color=#7f0055]void[/color][/b][color=#7f0055][/color] windowClosing(java.awt.event.WindowEvent e) {
  43. [color=#3f7f5f]//release all the resource
  44. [/color][color=#0000c0]xmlTree[/color] = [b][color=#7f0055]null[/color][/b][color=#7f0055][/color];
  45. [color=#0000c0]tn[/color] = [b][color=#7f0055]null[/color][/b][color=#7f0055][/color];
  46. }
  47. });
  48. setVisible([b][color=#7f0055]true[/color][/b][color=#7f0055][/color]);
  49.  
  50. [color=#3f7f5f]//add Listener to Print to Screen the xml tag selected
  51. [/color][color=#0000c0]xmlTree[/color].addTreeSelectionListener([b][color=#7f0055]new[/color][/b][color=#7f0055][/color] TreeSelectionListener() {
  52. [b][color=#7f0055]public[/color][/b][color=#7f0055][/color] [b][color=#7f0055]void[/color][/b][color=#7f0055][/color] valueChanged(TreeSelectionEvent evt) {
  53. [color=#3f7f5f]// Get all nodes whose selection status has changed
  54. [/color]TreePath[] paths = evt.getPaths();
  55. [color=#3f7f5f]// Print the last Path Component selected
  56. [/color]System.[i][color=#0000c0]out[/color][/i][color=#0000c0][/color].println([color=#2a00ff]"Path = "[/color] + evt.getPath().getLastPathComponent());
  57. [color=#0000c0]selectedNode[/color] = (DefaultMutableTreeNode) [color=#0000c0]xmlTree[/color].getLastSelectedPathComponent();
  58. }
  59. });
  60. }
  61.  
  62. [color=#3f7f5f]//Get Tag and element content
  63. [/color][b][color=#7f0055]private[/color][/b][color=#7f0055][/color] [b][color=#7f0055]void[/color][/b][color=#7f0055][/color] processElement(Element el, DefaultMutableTreeNode dmtn) {
  64. DefaultMutableTreeNode currentNode = [b][color=#7f0055]new[/color][/b][color=#7f0055][/color] DefaultMutableTreeNode(el.getName());
  65. String text = el.getTextNormalize();
  66.  
  67. [b][color=#7f0055]if[/color][/b][color=#7f0055][/color] ( (text != [b][color=#7f0055]null[/color][/b][color=#7f0055][/color]) && (!text.equals([color=#2a00ff]""[/color]))) {
  68. currentNode.add([b][color=#7f0055]new[/color][/b][color=#7f0055][/color] DefaultMutableTreeNode(text));
  69. }
  70.  
  71.  
  72. Iterator children = el.getChildren().iterator();
  73. [b][color=#7f0055]while[/color][/b][color=#7f0055][/color] (children.hasNext()) {
  74. processElement( (Element) children.next(), currentNode);
  75. }
  76. dmtn.add(currentNode);
  77. }
  78.  
  79. [b][color=#7f0055]private[/color][/b][color=#7f0055][/color] [b][color=#7f0055]void[/color][/b][color=#7f0055][/color] processAttributes(Element el, DefaultMutableTreeNode dmtn) {
  80. Iterator atts = el.getAttributes().iterator();
  81.  
  82. [b][color=#7f0055]while[/color][/b][color=#7f0055][/color] (atts.hasNext()) {
  83. Attribute att = (Attribute) atts.next();
  84. DefaultMutableTreeNode attNode = [b][color=#7f0055]new[/color][/b][color=#7f0055][/color] DefaultMutableTreeNode([color=#2a00ff]"@"[/color] + att.getName());
  85. attNode.add([b][color=#7f0055]new[/color][/b][color=#7f0055][/color] DefaultMutableTreeNode(att.getValue()));
  86. dmtn.add(attNode);
  87. }
  88. }
  89.  
  90. [b][color=#7f0055]private[/color][/b][color=#7f0055][/color] JTabbedPane tabbedPane(){
  91. JTabbedPane tp = [b][color=#7f0055]new[/color][/b][color=#7f0055][/color] JTabbedPane (JTabbedPane.[i][color=#0000c0]LEFT[/color][/i][color=#0000c0][/color]);
  92. tp.addTab([color=#2a00ff]"Operators"[/color], createPage1());
  93.  
  94. [b][color=#7f0055]return[/color][/b][color=#7f0055][/color] tp;
  95. }
  96.  
  97. [b][color=#7f0055]private[/color][/b][color=#7f0055][/color] JPanel createPage1(){
  98. JPanel pg = [b][color=#7f0055]new[/color][/b][color=#7f0055][/color] JPanel();
  99.  
  100. Box top = [b][color=#7f0055]new[/color][/b][color=#7f0055][/color] Box(BoxLayout.[i][color=#0000c0]Y_AXIS[/color][/i][color=#0000c0][/color]);
  101.  
  102.  
  103.  
  104. [color=#0000c0]primeButton[/color] = [b][color=#7f0055]new[/color][/b][color=#7f0055][/color] JButton([color=#2a00ff]"Prime(')"[/color]);
  105. [color=#0000c0]primeButton[/color].addActionListener([b][color=#7f0055]this[/color][/b][color=#7f0055][/color]);
  106.  
  107. top.add([color=#0000c0]primeButton[/color]);
  108. pg.add(top);
  109. [b][color=#7f0055]return[/color][/b][color=#7f0055][/color] pg;
  110. }
  111.  
  112. [b][color=#7f0055]public[/color][/b][color=#7f0055][/color] [b][color=#7f0055]void[/color][/b][color=#7f0055][/color] actionPerformed(ActionEvent e){
  113. DefaultMutableTreeNode tn1;
  114. [color=#3f5fbf]/***[/color] [color=#3f5fbf]1.[/color] [color=#3f5fbf]Prime***/
  115. [/color][b][color=#7f0055]if[/color][/b][color=#7f0055][/color](e.getSource().equals([color=#0000c0]showButton[/color])){
  116. System.[i][color=#0000c0]out[/color][/i][color=#0000c0][/color].println([color=#2a00ff]"showButton is clicked."[/color]);
  117. tn1 = [b][color=#7f0055]new[/color][/b][color=#7f0055][/color] DefaultMutableTreeNode([color=#2a00ff]"Result: Hyper Tree"[/color]);
  118.  
  119. processPrime([color=#0000c0]xmlDoc[/color].getRootElement(), tn1, 1);
  120.  
  121. [color=#0000c0]resultTree[/color] = [b][color=#7f0055]new[/color][/b][color=#7f0055][/color] JTree();
  122. ( (DefaultTreeModel) [color=#0000c0]resultTree[/color].getModel()).setRoot(tn1);
  123. 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]);
  124.  
  125. setVisible([b][color=#7f0055]true[/color][/b][color=#7f0055][/color]);
  126.  
  127.  
  128. }
  129. [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]
  130. [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])){
  131.  
  132. System.[i][color=#0000c0]out[/color][/i][color=#0000c0][/color].println([color=#2a00ff]"primeButton is clicked."[/color]);
  133. tn1 = [b][color=#7f0055]new[/color][/b][color=#7f0055][/color] DefaultMutableTreeNode([color=#2a00ff]"Result: Hyper Tree"[/color]);
  134. [color=#3f7f5f]//DefaultMutableTreeNode cloneNode = (DefaultMutableTreeNode) selectedNode.clone();
  135. [/color][b][color=#7f0055]int[/color][/b][color=#7f0055][/color] childCount = [color=#0000c0]selectedNode[/color].getChildCount();
  136. System.[i][color=#0000c0]out[/color][/i][color=#0000c0][/color].println([color=#2a00ff]"loops = "[/color]+ childCount);
  137. DefaultMutableTreeNode newChild = [b][color=#7f0055]new[/color][/b][color=#7f0055][/color] DefaultMutableTreeNode();
  138. [b][color=#7f0055]for[/color][/b][color=#7f0055][/color]([b][color=#7f0055]int[/color][/b][color=#7f0055][/color] i = 0; i < childCount; i++){
  139. newChild = (DefaultMutableTreeNode)( [color=#0000c0]selectedNode[/color].getChildAt(0));
  140. tn1.add(newChild);
  141.  
  142.  
  143. }
  144.  
  145. [color=#0000c0]resultTree[/color] = [b][color=#7f0055]new[/color][/b][color=#7f0055][/color] JTree();
  146. ( (DefaultTreeModel) [color=#0000c0]resultTree[/color].getModel()).setRoot(tn1);
  147. 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]);
  148. setVisible([b][color=#7f0055]true[/color][/b][color=#7f0055][/color]);
  149.  
  150. }
  151. }
  152.  
  153. [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) {
  154.  
  155. Iterator children = el.getChildren().iterator();
  156. [b][color=#7f0055]while[/color][/b][color=#7f0055][/color] (children.hasNext()) {
  157.  
  158.  
  159. Object childObject = children.next();
  160. DefaultMutableTreeNode currentNode = [b][color=#7f0055]new[/color][/b][color=#7f0055][/color] DefaultMutableTreeNode(((Element) childObject).getName());
  161. processElement( (Element) childObject, dmtn);
  162.  
  163. }
  164. lev--;
  165. }
  166.  
  167. [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) {
  168.  
  169. lev--;
  170. }
  171.  
  172.  
  173. [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 {
  174. SAXBuilder builder = [b][color=#7f0055]new[/color][/b][color=#7f0055][/color] SAXBuilder();
  175. 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]));
  176. XMLTreeViewer viewer = [b][color=#7f0055]new[/color][/b][color=#7f0055][/color] XMLTreeViewer(doc);
  177. viewer.addWindowListener([b][color=#7f0055]new[/color][/b][color=#7f0055][/color] java.awt.event.WindowAdapter() {
  178. [b][color=#7f0055]public[/color][/b][color=#7f0055][/color] [b][color=#7f0055]void[/color][/b][color=#7f0055][/color] windowClosing(java.awt.event.WindowEvent e) {
  179. System.[i]exit[/i](0);
  180. }
  181. });
  182. }
  183. }
  184.  
  185.  

I have declared an operator called as "prime" which will display the subtree of the selected node in the original tree in a new frame. But when I am performing this operator on the original tree, it is also modifying the original tree thus preventing me to perform the same operation on the same node. I need to restart the application in order to perform the operation again on the same node.

Can you plese let me know where I am going wrong or where to modify the code such that performing the operation on the original tree doesnt modify it.

Thanks!!
Reply With Quote  
Join Date: Jun 2007
Posts: 10
Reputation: flavour_of_bru is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
flavour_of_bru flavour_of_bru is offline Offline
Newbie Poster

Re: Help needed with JTree

  #8  
Oct 18th, 2007
Sorry......the code given above could not display properly. So I am resending the code back in proper form.

  1.  
  2.  
  3. import java.awt.*;
  4. import javax.swing.*;
  5. import javax.swing.tree.*;
  6. import javax.swing.event.*;
  7. import java.util.Iterator;
  8. import java.util.List;
  9. import org.jdom.*;
  10. import org.jdom.input.SAXBuilder;
  11. import java.io.File;
  12. import java.awt.event.*;
  13. public class XMLTreeViewer extends JFrame implements ActionListener{
  14. //The JTree to display the XML
  15. private JTree xmlTree;
  16. private JTree resultTree;
  17. JButton showButton ;
  18. JButton primeButton;
  19.  
  20. //The XML document to be output to the JTree
  21. private Document xmlDoc;
  22. DefaultMutableTreeNode tn,selectedNode;
  23.  
  24. public XMLTreeViewer(Document doc) {
  25. super("WebOQL Homework");
  26. this.xmlDoc = doc; //document
  27. setSize(600, 450);
  28. tn = new DefaultMutableTreeNode("Hyper Tree");
  29. initialize();
  30. }
  31.  
  32. private void initialize() {
  33. xmlTree = new JTree();
  34. xmlTree.setName("HyperTree");
  35. getContentPane().add(new JScrollPane(xmlTree), BorderLayout.LINE_START);
  36. getContentPane().add(tabbedPane(),BorderLayout.LINE_END);
  37. processElement(xmlDoc.getRootElement(), tn); //create a tree
  38. ( (DefaultTreeModel) xmlTree.getModel()).setRoot(tn);
  39. addWindowListener(new java.awt.event.WindowAdapter() {
  40. public void windowClosing(java.awt.event.WindowEvent e) {
  41. //release all the resource
  42. xmlTree = null;
  43. tn = null;
  44. }
  45. });
  46. setVisible(true);
  47.  
  48. //add Listener to Print to Screen the xml tag selected
  49. xmlTree.addTreeSelectionListener(new TreeSelectionListener() {
  50. public void valueChanged(TreeSelectionEvent evt) {
  51. // Get all nodes whose selection status has changed
  52. TreePath[] paths = evt.getPaths();
  53. // Print the last Path Component selected
  54. System.out.println("Path = " + evt.getPath().getLastPathComponent());
  55. selectedNode = (DefaultMutableTreeNode) xmlTree.getLastSelectedPathComponent();
  56. }
  57. });
  58. }
  59.  
  60. //Get Tag and element content
  61. private void processElement(Element el, DefaultMutableTreeNode dmtn) {
  62. DefaultMutableTreeNode currentNode = new DefaultMutableTreeNode(el.getName());
  63. String text = el.getTextNormalize();
  64. if ( (text != null) && (!text.equals(""))) {
  65. currentNode.add(new DefaultMutableTreeNode(text));
  66. }
  67.  
  68.  
  69. Iterator children = el.getChildren().iterator();
  70. while (children.hasNext()) {
  71. processElement( (Element) children.next(), currentNode);
  72. }
  73. dmtn.add(currentNode);
  74. }
  75. private void processAttributes(Element el, DefaultMutableTreeNode dmtn) {
  76. Iterator atts = el.getAttributes().iterator();
  77. while (atts.hasNext()) {
  78. Attribute att = (Attribute) atts.next();
  79. DefaultMutableTreeNode attNode = new DefaultMutableTreeNode("@" + att.getName());
  80. attNode.add(new DefaultMutableTreeNode(att.getValue()));
  81. dmtn.add(attNode);
  82. }
  83. }
  84.  
  85. private JTabbedPane tabbedPane(){
  86. JTabbedPane tp = new JTabbedPane (JTabbedPane.LEFT);
  87. tp.addTab("Operators", createPage1());
  88.  
  89. return tp;
  90. }
  91.  
  92. private JPanel createPage1(){
  93. JPanel pg = new JPanel();
  94.  
  95. Box top = new Box(BoxLayout.Y_AXIS);
  96.  
  97.  
  98.  
  99. primeButton = new JButton("Prime(')");
  100. primeButton.addActionListener(this);
  101.  
  102. top.add(primeButton);
  103. pg.add(top);
  104. return pg;
  105. }
  106.  
  107. public void actionPerformed(ActionEvent e){
  108. DefaultMutableTreeNode tn1;
  109. /*** 1. Prime***/
  110. if(e.getSource().equals(showButton)){
  111. System.out.println("showButton is clicked.");
  112. tn1 = new DefaultMutableTreeNode("Result: Hyper Tree");
  113.  
  114. processPrime(xmlDoc.getRootElement(), tn1, 1);
  115.  
  116. resultTree = new JTree();
  117. ( (DefaultTreeModel) resultTree.getModel()).setRoot(tn1);
  118. getContentPane().add(new JScrollPane(resultTree), BorderLayout.CENTER);
  119.  
  120. setVisible(true);
  121.  
  122.  
  123. }
  124. /*** 2. Prime from the tree ***/
  125. else if(e.getSource().equals(primeButton)){
  126.  
  127. System.out.println("primeButton is clicked.");
  128. tn1 = new DefaultMutableTreeNode("Result: Hyper Tree");
  129. //DefaultMutableTreeNode cloneNode = (DefaultMutableTreeNode) selectedNode.clone();
  130. int childCount = selectedNode.getChildCount();
  131. System.out.println("loops = "+ childCount);
  132. DefaultMutableTreeNode newChild = new DefaultMutableTreeNode();
  133. for(int i = 0; i < childCount; i++){
  134. newChild = (DefaultMutableTreeNode)( selectedNode.getChildAt(0));
  135. tn1.add(newChild);
  136.  
  137. }
  138.  
  139. resultTree = new JTree();
  140. ( (DefaultTreeModel) resultTree.getModel()).setRoot(tn1);
  141. getContentPane().add(new JScrollPane(resultTree), BorderLayout.CENTER);
  142. setVisible(true);
  143.  
  144. }
  145. }
  146.  
  147. private void processPrime(Element el, DefaultMutableTreeNode dmtn, int lev) {
  148. Iterator children = el.getChildren().iterator();
  149. while (children.hasNext()) {
  150.  
  151.  
  152. Object childObject = children.next();
  153. DefaultMutableTreeNode currentNode = new DefaultMutableTreeNode(((Element) childObject).getName());
  154. processElement( (Element) childObject, dmtn);
  155.  
  156. }
  157. lev--;
  158. }
  159.  
  160. private void processTail(Element el, DefaultMutableTreeNode dmtn, int lev) {
  161.  
  162. lev--;
  163. }
  164.  
  165. public static void main(String args[]) throws Exception {
  166. SAXBuilder builder = new SAXBuilder();
  167. Document doc = builder.build(new File("C://Users//Brugu Maharishi//Documents//Google Talk Received Files//cs3.html"));
  168. XMLTreeViewer viewer = new XMLTreeViewer(doc);
  169. viewer.addWindowListener(new java.awt.event.WindowAdapter() {
  170. public void windowClosing(java.awt.event.WindowEvent e) {
  171. System.exit(0);
  172. }
  173. });
  174. }
  175. }
  176.  
  177.  
  178.  
  179.  
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,090
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 307
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Sensei

Re: Help needed with JTree

  #9  
Oct 18th, 2007
The modification is occurring because you are adding all of the child nodes of the selected node to a new root. The add(MutableTreeNode) method states that it disconnects the added node from its parent and makes it a child of the node it's added to. I see that you tried to use clone() on the selected node, but that fails because they implemented clone() as a shallow copy operation. It does not copy the parent nor children references (deep copy).

How to fix it depends on what you intend to do with the "prime" subtree. If you do not need to modify it without changing the original tree and you can do without the new root node "ResultTree", then the changes I put in bold in the code below will work for you.

If you absolutely need a separate copy of that structure, then you will have to write your own deep copy method that clones a node recursively by cloning that node and its children and all subsequent children of those children. That's not really hard to do, but if you don't need the separate copy you don't need to bother with it.
Originally Posted by flavour_of_bru View Post
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);
        }
      });
    }
  }
 
 
 
 
Reply With Quote  
Join Date: Jun 2007
Posts: 10
Reputation: flavour_of_bru is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
flavour_of_bru flavour_of_bru is offline Offline
Newbie Poster

Re: Help needed with JTree

  #10  
Oct 18th, 2007
Thanks for replying to my queries.

I have one more question.
1. Is it possible for me to display only the tree structure without displaying the leaf nodes (I dont want to also display the root node)
2. I just want to get all the non-leaf nodes without the root node and display all non-leaf nodes(not needed in tree form)

Do we have any functions for retrieving the non-leaf nodes from the given tree?

Please help!!
Thanks!!
Reply With Quote