943,696 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1820
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 16th, 2007
0

Help needed with JTree

Expand Post »
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!!!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
flavour_of_bru is offline Offline
11 posts
since Jun 2007
Oct 16th, 2007
0

Re: Help needed with JTree

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,757 posts
since May 2007
Oct 16th, 2007
0

Re: Help needed with JTree

Java Syntax (Toggle Plain Text)
  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!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
flavour_of_bru is offline Offline
11 posts
since Jun 2007
Oct 17th, 2007
0

Re: Help needed with JTree

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
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,757 posts
since May 2007
Oct 17th, 2007
0

Re: Help needed with JTree

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!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
flavour_of_bru is offline Offline
11 posts
since Jun 2007
Oct 17th, 2007
0

Re: Help needed with JTree

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,757 posts
since May 2007
Oct 18th, 2007
0

Re: Help needed with JTree

Java Syntax (Toggle Plain Text)
  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. }

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!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
flavour_of_bru is offline Offline
11 posts
since Jun 2007
Oct 18th, 2007
0

Re: Help needed with JTree

Sorry......the code given above could not display properly. So I am resending the code back in proper form.

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
flavour_of_bru is offline Offline
11 posts
since Jun 2007
Oct 18th, 2007
0

Re: Help needed with JTree

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.
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);
        }
      });
    }
  }
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,757 posts
since May 2007
Oct 18th, 2007
0

Re: Help needed with JTree

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!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
flavour_of_bru is offline Offline
11 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: HELP: DateComponent error
Next Thread in Java Forum Timeline: Fraction Calculator





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC