1  import java.awt.*;
  2  import javax.swing.*;
  3  import javax.swing.tree.*;
  4  import java.util.*;
  5  
  6  public class TreeNodeDemo extends JApplet {
  7    public TreeNodeDemo() {
  8      // Create the first tree
  9      DefaultMutableTreeNode root, europe, northAmerica, us;
 10  
 11      europe = new DefaultMutableTreeNode("Europe");
 12      europe.add(new DefaultMutableTreeNode("UK"));
 13      europe.add(new DefaultMutableTreeNode("Germany"));
 14      europe.add(new DefaultMutableTreeNode("France"));
 15      europe.add(new DefaultMutableTreeNode("Norway"));
 16  
 17      northAmerica = new DefaultMutableTreeNode("North America");
 18      us = new DefaultMutableTreeNode("US");
 19      us.add(new DefaultMutableTreeNode("California"));
 20      us.add(new DefaultMutableTreeNode("Texas"));
 21      us.add(new DefaultMutableTreeNode("New York"));
 22      us.add(new DefaultMutableTreeNode("Florida"));
 23      us.add(new DefaultMutableTreeNode("Illinois"));
 24      northAmerica.add(us);
 25      northAmerica.add(new DefaultMutableTreeNode("Canada"));
 26  
 27      root = new DefaultMutableTreeNode("World");
 28      root.add(europe);
 29      root.add(northAmerica);
 30  
 31      JPanel panel = new JPanel();
 32      panel.setLayout(new GridLayout(1, 2));
 33      panel.add(new JScrollPane(new JTree(root)));
 34      panel.add(new JScrollPane(new JTree(new DefaultTreeModel(root))));
 35  
 36      JTextArea jtaMessage = new JTextArea();
 37      jtaMessage.setWrapStyleWord(true);
 38      jtaMessage.setLineWrap(true);
 39      add(new JSplitPane(JSplitPane.VERTICAL_SPLIT,
 40        panel, new JScrollPane(jtaMessage)), BorderLayout.CENTER);
 41  
 42      // Get tree information
 43      jtaMessage.append("Depth of the node US is " + us.getDepth());
 44      jtaMessage.append("\nLevel of the node US is " + us.getLevel());
 45      jtaMessage.append("\nFirst child of the root is " +
 46        root.getFirstChild());
 47      jtaMessage.append("\nFirst leaf of the root is " +
 48        root.getFirstLeaf());
 49      jtaMessage.append("\nNumber of the children of the root is " +
 50        root.getChildCount());
 51      jtaMessage.append("\nNumber of leaves in the tree is " +
 52        root.getLeafCount());
 53      String breadthFirstSearchResult = "";
 54  
 55      // Breadth-first traversal
 56      Enumeration bf = root.breadthFirstEnumeration();
 57      while (bf.hasMoreElements())
 58        breadthFirstSearchResult += bf.nextElement().toString() + " ";
 59      jtaMessage.append("\nBreath-first traversal from the root is "
 60        + breadthFirstSearchResult);
 61    }
 62  
 63    public static void main(String[] args) {
 64      TreeNodeDemo applet = new TreeNodeDemo();
 65      JFrame frame = new JFrame();
 66      //EXIT_ON_CLOSE == 3
 67      frame.setDefaultCloseOperation(3);
 68      frame.setTitle("TreeNodeDemo");
 69      frame.getContentPane().add(applet, BorderLayout.CENTER);
 70      applet.init();
 71      applet.start();
 72      frame.setSize(400,320);
 73      frame.setLocationRelativeTo(null);
 74      frame.setVisible(true);
 75    }
 76  }