{"id":27190,"date":"2021-05-13T23:10:07","date_gmt":"2021-05-14T03:10:07","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/jtree-examples-programming-code-examples-java-j2ee-j2me-advanced-swing\/"},"modified":"2021-05-13T23:10:07","modified_gmt":"2021-05-14T03:10:07","slug":"jtree-examples-programming-code-examples-java-j2ee-j2me-advanced-swing","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=27190","title":{"rendered":"JTree Examples #Programming Code Examples #Java\/J2EE\/J2ME #Advanced Swing"},"content":{"rendered":"<pre>\nSimpleTree.java Basic tree built out of DefaultMutableTreeNodes. A DefualtMutableTreeNode is a starting point for a root node, in which children nodes can be added. \nimport java.awt.*;\nimport javax.swing.*;\nimport javax.swing.tree.*;\n\n\/** Example tree built out of DefaultMutableTreeNodes. \n *\n *\/\n\npublic class SimpleTree extends JFrame {\n  public static void main(String[] args) {\n    new SimpleTree();\n  }\n \n  public SimpleTree() {\n    super(&quot;Creating a Simple JTree&quot;);\n    WindowUtilities.setNativeLookAndFeel();\n    addWindowListener(new ExitListener());\n    Container content = getContentPane();\n    Object[] hierarchy =\n      { &quot;javax.swing&quot;,\n        &quot;javax.swing.border&quot;,\n        &quot;javax.swing.colorchooser&quot;,\n        &quot;javax.swing.event&quot;,\n        &quot;javax.swing.filechooser&quot;,\n        new Object[] { &quot;javax.swing.plaf&quot;,\n                       &quot;javax.swing.plaf.basic&quot;,\n                       &quot;javax.swing.plaf.metal&quot;,\n                       &quot;javax.swing.plaf.multi&quot; },\n        &quot;javax.swing.table&quot;,\n        new Object[] { &quot;javax.swing.text&quot;,\n                       new Object[] { &quot;javax.swing.text.html&quot;,\n                                     &quot;javax.swing.text.html.parser&quot; },\n                       &quot;javax.swing.text.rtf&quot; },\n        &quot;javax.swing.tree&quot;,\n        &quot;javax.swing.undo&quot; };\n    DefaultMutableTreeNode root = processHierarchy(hierarchy);\n    JTree tree = new JTree(root);\n    content.add(new JScrollPane(tree), BorderLayout.CENTER);\n    setSize(275, 300);\n    setVisible(true);\n  }\n\n  \/** Small routine that will make a node out of the first entry\n   *  in the array, then make nodes out of subsequent entries\n   *  and make them child nodes of the first one. The process \n   *  is repeated recursively for entries that are arrays.\n   *\/\n\n  private DefaultMutableTreeNode processHierarchy(\n                                           Object[] hierarchy) {\n    DefaultMutableTreeNode node =\n      new DefaultMutableTreeNode(hierarchy[0]);\n    DefaultMutableTreeNode child;\n    for(int i=1; i 0) {\n      try {\n        n = Integer.parseInt(args[0]);\n      } catch(NumberFormatException nfe) {\n        System.out.println(\n          &quot;Can't parse number; using default of &quot; + n);\n      }\n   }\n    new DynamicTree(n);\n  }\n \n  public DynamicTree(int n) {\n    super(&quot;Creating a Dynamic JTree&quot;);\n    WindowUtilities.setNativeLookAndFeel();\n    addWindowListener(new ExitListener());\n    Container content = getContentPane();\n    JTree tree = new JTree(new OutlineNode(1, n));\n    content.add(new JScrollPane(tree), BorderLayout.CENTER);\n    setSize(300, 475);\n    setVisible(true);\n  }\n}\n\n\n    * OutlineNode.java A simple tree node that builds its children.\nimport java.awt.*;\nimport javax.swing.*;\nimport javax.swing.tree.*;\n\n\/** Simple TreeNode that builds children on the fly.\n *  The key idea is that getChildCount is always called before\n *  any actual children are requested. That way, getChildCount \n *  builds the children if they don't already exist.\n *  <p>\n *  In this case, it just builds an &quot;outline&quot; tree. I.e.,\n *  if the root is current node is &quot;x&quot;, the children are\n *  &quot;x.0&quot;, &quot;x.1&quot;, &quot;x.2&quot;, and &quot;x.3&quot;.\n * <\/p><p>\n *\n *\/\n\npublic class OutlineNode extends DefaultMutableTreeNode {\n  private boolean areChildrenDefined = false;\n  private int outlineNum;\n  private int numChildren;\n\n  public OutlineNode(int outlineNum, int numChildren) {\n    this.outlineNum = outlineNum;\n    this.numChildren = numChildren;\n  }\n  \n  public boolean isLeaf() {\n    return(false);\n  }\n\n  public int getChildCount() {\n    if (!areChildrenDefined) {\n      defineChildNodes();\n    }\n    return(super.getChildCount());\n  }\n\n  private void defineChildNodes() {\n    \/\/ You must set the flag before defining children if you\n    \/\/ use &quot;add&quot; for the new children. Otherwise, you get an \n    \/\/ infinite recursive loop since add results in a call \n    \/\/ to getChildCount. However, you could use &quot;insert&quot; in such \n    \/\/ a case.\n    areChildrenDefined = true;\n    for(int i=0; i&lt;numchildren ; i++) {\n      add(new OutlineNode(i+1, numChildren));\n    }\n  }\n\n  public String toString() {\n    TreeNode parent = getParent();\n    if (parent == null) {\n      return(String.valueOf(outlineNum));\n    } else {\n      return(parent.toString() + &quot;.&quot; + outlineNum);\n    }\n  }\n}\n**\/\/\nCustomIcons.java Demonstrates displaying custom icons for the nodes of a tree.\nimport java.awt.*;\nimport java.awt.event.*;\nimport javax.swing.*;\nimport javax.swing.tree.*;\n\n\/** JTree with missing or custom icons at the tree nodes. \n *\n  *\/\n\npublic class CustomIcons extends JFrame {\n  public static void main(String[] args) {\n    new CustomIcons();\n  }\n\n  private Icon customOpenIcon = \n            new ImageIcon(&quot;images\/Circle_1.gif&quot;);\n  private Icon customClosedIcon = \n            new ImageIcon(&quot;images\/Circle_2.gif&quot;);\n  private Icon customLeafIcon = \n            new ImageIcon(&quot;images\/Circle_3.gif&quot;);\n  \n  public CustomIcons() {\n    super(&quot;JTree Selections&quot;);\n    WindowUtilities.setNativeLookAndFeel();\n    addWindowListener(new ExitListener());\n    Container content = getContentPane();\n    content.setLayout(new FlowLayout());\n    DefaultMutableTreeNode root =\n      new DefaultMutableTreeNode(&quot;Root&quot;);\n    DefaultMutableTreeNode child;\n    DefaultMutableTreeNode grandChild;\n    for(int childIndex=1; childIndex&lt;4; childIndex++) {\n      child = new DefaultMutableTreeNode(&quot;Child &quot; + childIndex);\n      root.add(child);\n      for(int grandChildIndex=1; grandChildIndex&lt;4; \n                                 grandChildIndex++) {\n        grandChild =\n          new DefaultMutableTreeNode(&quot;Grandchild &quot; + \n                                     childIndex +\n                                     &quot;.&quot; + grandChildIndex);\n        child.add(grandChild);\n      }\n    }\n\n    JTree tree1 = new JTree(root);\n    tree1.expandRow(1); \/\/ Expand children to illustrate leaf icons.\n    JScrollPane pane1 = new JScrollPane(tree1);\n    pane1.setBorder(\n            BorderFactory.createTitledBorder(&quot;Standard Icons&quot;));\n    content.add(pane1);\n\n    JTree tree2 = new JTree(root);\n    \/\/ Expand children to illustrate leaf icons.\n    tree2.expandRow(2); \n    DefaultTreeCellRenderer renderer2 = \n                              new DefaultTreeCellRenderer();\n    renderer2.setOpenIcon(null);\n    renderer2.setClosedIcon(null);\n    renderer2.setLeafIcon(null);\n    tree2.setCellRenderer(renderer2);\n    JScrollPane pane2 = new JScrollPane(tree2);\n    pane2.setBorder(\n            BorderFactory.createTitledBorder(&quot;No Icons&quot;));\n    content.add(pane2);\n\n    JTree tree3 = new JTree(root);\n    \/\/ Expand children to illustrate leaf icons.\n    tree3.expandRow(3); \n    DefaultTreeCellRenderer renderer3 = \n                              new DefaultTreeCellRenderer();\n    renderer3.setOpenIcon(customOpenIcon);\n    renderer3.setClosedIcon(customClosedIcon);\n    renderer3.setLeafIcon(customLeafIcon);\n    tree3.setCellRenderer(renderer3);\n    JScrollPane pane3 = new JScrollPane(tree3);\n    pane3.setBorder(\n           BorderFactory.createTitledBorder(&quot;Custom Icons&quot;));\n    content.add(pane3);\n\n    pack();\n    setVisible(true);\n  }\n}\n**\/\/\n<\/p><\/pre>\n<p>Note: Brought from our old site: http:\/\/www.salearningschool.com\/example_codes\/ on Jan 2nd, 2017 From: http:\/\/sitestree.com\/?p=10300<br \/> Categories:Programming Code Examples, Java\/J2EE\/J2ME, Advanced Swing<br \/>Tags:Java\/J2EE\/J2MEAdvanced Swing<br \/> Post Data:2017-01-02 16:04:31<\/p>\n<p>\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com\/' target='new' rel=\"noopener\">https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\t(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http:\/\/Training.SitesTree.com' target='new' rel=\"noopener\"> http:\/\/Training.SitesTree.com<\/a><br \/>\n\t\tIn Bengali: <a href='http:\/\/Bangla.SaLearningSchool.com' target='new' rel=\"noopener\">http:\/\/Bangla.SaLearningSchool.com<\/a><br \/>\n\t\t<a href='http:\/\/SitesTree.com' target='new' rel=\"noopener\">http:\/\/SitesTree.com<\/a><br \/>\n\t\t8112223 Canada Inc.\/JustEtc: <a href='http:\/\/JustEtc.net' target='new' rel=\"noopener\">http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) <\/a><br \/>\n\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com'> https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\tMedium: <a href='https:\/\/medium.com\/@SayedAhmedCanada' target='new' rel=\"noopener\"> https:\/\/medium.com\/@SayedAhmedCanada <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>SimpleTree.java Basic tree built out of DefaultMutableTreeNodes. A DefualtMutableTreeNode is a starting point for a root node, in which children nodes can be added. import java.awt.*; import javax.swing.*; import javax.swing.tree.*; \/** Example tree built out of DefaultMutableTreeNodes. * *\/ public class SimpleTree extends JFrame { public static void main(String[] args) { new SimpleTree(); } public &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=27190\">Continue reading<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1917],"tags":[],"class_list":["post-27190","post","type-post","status-publish","format-standard","hentry","category-fromsitestree-com","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":10267,"url":"http:\/\/bangla.sitestree.com\/?p=10267","url_meta":{"origin":27190,"position":0},"title":"JTree Examples","author":"","date":"August 26, 2015","format":false,"excerpt":"SimpleTree.java Basic tree built out of DefaultMutableTreeNodes. A DefualtMutableTreeNode is a starting point for a root node, in which children nodes can be added. import java.awt.*; import javax.swing.*; import javax.swing.tree.*; \/** Example tree built out of DefaultMutableTreeNodes. \u00a0* \u00a0*\/ public class SimpleTree extends JFrame { \u00a0 public static void main(String[]\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27194,"url":"http:\/\/bangla.sitestree.com\/?p=27194","url_meta":{"origin":27190,"position":1},"title":"Printing in Java 2 #Programming Code Examples #Java\/J2EE\/J2ME #Advanced Swing","author":"Author-Check- Article-or-Video","date":"May 13, 2021","format":false,"excerpt":"* o PrintExample.java Demonstrates printing a Graphics2D object. import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.print.*; \/** An example of a printable window in Java 1.2. The key point * here is that any component is printable in Java 1.2. * However, you have to be careful to turn off\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27192,"url":"http:\/\/bangla.sitestree.com\/?p=27192","url_meta":{"origin":27190,"position":2},"title":"JTable Examples #Programming Code Examples #Java\/J2EE\/J2ME #Advanced Swing","author":"Author-Check- Article-or-Video","date":"May 13, 2021","format":false,"excerpt":"# JTableSimpleExample.java Simple table that takes column names and data from arrays of Strings. import java.awt.*; import javax.swing.*; \/** Simple JTable example that uses a String array for the * table header and table data. * *\/ public class JTableSimpleExample extends JFrame { public static void main(String[] args) { new\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10265,"url":"http:\/\/bangla.sitestree.com\/?p=10265","url_meta":{"origin":27190,"position":3},"title":"JList Examples","author":"","date":"August 26, 2015","format":false,"excerpt":"All examples, except for FileTransfer use WindowUtilities.java and ExitListener.java. WindowUtilities.java: import javax.swing.*; import java.awt.*;\u00a0\u00a0 \/\/ For Color and Container classes. \/** A few utilities that simplify using windows in Swing. \u00a0* \u00a0 *\/ public class WindowUtilities { \u00a0 \/** Tell system to use native look and feel, as in previous\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27188,"url":"http:\/\/bangla.sitestree.com\/?p=27188","url_meta":{"origin":27190,"position":4},"title":"JList Examples #Programming Code Examples #Java\/J2EE\/J2ME #Advanced Swing","author":"Author-Check- Article-or-Video","date":"May 13, 2021","format":false,"excerpt":"All examples, except for FileTransfer use WindowUtilities.java and ExitListener.java. WindowUtilities.java: import javax.swing.*; import java.awt.*; \/\/ For Color and Container classes. \/** A few utilities that simplify using windows in Swing. * *\/ public class WindowUtilities { \/** Tell system to use native look and feel, as in previous * releases.\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26856,"url":"http:\/\/bangla.sitestree.com\/?p=26856","url_meta":{"origin":27190,"position":5},"title":"DOM example that represents the basic structure of an XML document as a JTree #Programming Code Examples #Java\/J2EE\/J2ME #JavaScript","author":"Author-Check- Article-or-Video","date":"May 3, 2021","format":false,"excerpt":"\/\/XMLTree.java \/\/Uses the following files Uses the following files: * XMLFrame.java:Swing application to select an XML document and display in a JTree. ExtensionFileFilter.java Allows you to specify which file extensions will be displayed in a JFileChooser. test.xml Default file loaded if none selected by user. perennials.xml and perennials.dtd Data on\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/27190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=27190"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/27190\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=27190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=27190"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=27190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}