Swing Tree : JTree Node « Swing « Java Tutorial






Swing Tree
import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreePath;

public class SwingTree extends JFrame {
  JTextField textField = new JTextField();

  JScrollPane scrollPane = new JScrollPane();

  JTree tree;

  Renderer renderer = new Renderer();

  DefaultMutableTreeNode nba = new DefaultMutableTreeNode("National Basketball Association");

  DefaultMutableTreeNode western = new DefaultMutableTreeNode("Western Conference");

  DefaultMutableTreeNode pacific = new DefaultMutableTreeNode("Pacific Division Teams");


  DefaultMutableTreeNode midwest = new DefaultMutableTreeNode("Midwest Division Teams");

  DefaultMutableTreeNode denver = new DefaultMutableTreeNode("Denver");

  DefaultMutableTreeNode eastern = new DefaultMutableTreeNode("Eastern Conference");

  DefaultMutableTreeNode atlantic = new DefaultMutableTreeNode("Atlantic Division Teams");
  DefaultMutableTreeNode central = new DefaultMutableTreeNode("Central Division Teams");

  public SwingTree() {
    nba.add(western);
    nba.add(eastern);
    western.add(pacific);
    western.add(midwest);
    eastern.add(atlantic);
    eastern.add(central);
    pacific.add(new DefaultMutableTreeNode("Los Angeles (Lakers)"));
    pacific.add(new DefaultMutableTreeNode("Los Angeles (Clippers)"));
    pacific.add(new DefaultMutableTreeNode("San Francisco"));
    pacific.add(new DefaultMutableTreeNode("Seattle"));
    pacific.add(new DefaultMutableTreeNode("Phoenix"));
    pacific.add(new DefaultMutableTreeNode("Portland"));
    pacific.add(new DefaultMutableTreeNode("Sacramento"));
    
    
    midwest.add(new DefaultMutableTreeNode("Utah"));
    midwest.add(new DefaultMutableTreeNode("San Antonio"));
    midwest.add(new DefaultMutableTreeNode("Houston"));
    midwest.add(new DefaultMutableTreeNode("Minnesota"));
    midwest.add(new DefaultMutableTreeNode("Vancouver"));
    midwest.add(new DefaultMutableTreeNode("Dallas"));
   
    midwest.add(denver);
    atlantic.add(new DefaultMutableTreeNode("Miami"));
    atlantic.add(new DefaultMutableTreeNode("New York"));
    atlantic.add(new DefaultMutableTreeNode("New Jersey"));
    atlantic.add(new DefaultMutableTreeNode("Washington"));
    atlantic.add(new DefaultMutableTreeNode("Orlando"));
    atlantic.add(new DefaultMutableTreeNode("Boston"));
    atlantic.add(new DefaultMutableTreeNode("Philadelphia"));

    
    central.add(new DefaultMutableTreeNode("Chicago"));
    central.add(new DefaultMutableTreeNode("Indiana"));
    central.add(new DefaultMutableTreeNode("Charlotte"));
    central.add(new DefaultMutableTreeNode("Atlanta"));
    central.add(new DefaultMutableTreeNode("Cleveland"));
    central.add(new DefaultMutableTreeNode("Detroit"));
    central.add(new DefaultMutableTreeNode("Milwaukee"));
    central.add(new DefaultMutableTreeNode("Toronto"));
    tree = new JTree(nba);

    getContentPane().setLayout(new BorderLayout());
    tree.setCellRenderer(renderer);
    tree.addTreeSelectionListener(new TreeHandler());
    scrollPane.getViewport().add(tree);
    getContentPane().add("Center", scrollPane);
    getContentPane().add("South", textField);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setSize(500, 500);
    setVisible(true);
  }

  public static void main(String[] args) {
    SwingTree app = new SwingTree();
  }

  public class TreeHandler implements TreeSelectionListener {
    public void valueChanged(TreeSelectionEvent e) {
      TreePath path = e.getPath();
      String text = path.getPathComponent(path.getPathCount() - 1).toString();
      if (path.getPathCount() > 3) {
        text += ": ";
        text += Integer.toString((int) (Math.random() * 50)) + " Wins ";
        text += Integer.toString((int) (Math.random() * 50)) + " Losses";
      }
      textField.setText(text);
    }
  }
}
class Renderer extends JLabel implements TreeCellRenderer {
  public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
      boolean expanded, boolean leaf, int row, boolean hasFocus) {
    setText(value.toString() + "                   ");
    return this;
  }
}








14.67.JTree Node
14.67.1.Get child count, depth, leaf count
14.67.2.Adding a Node to a JTree Component
14.67.3.Delete tree node
14.67.4.Removing a Node to a JTree Component
14.67.5.Expression TreeExpression Tree
14.67.6.Swing TreeSwing Tree
14.67.7.JTree node mouse click event
14.67.8.A JTree subclass that displays the tree of AWT or Swing component that make up a GUI
14.67.9.Tree with custom icon
14.67.10.CheckBox Tree node
14.67.11.Get path for all expanded or not expanded tree pathes
14.67.12.Converting All Nodes in a JTree Component to a TreePath Array
14.67.13.Expanding or Collapsing All Nodes in a JTree Component
14.67.14.JTree root cannot be removed with removeNodeFromParent(), use DefaultTreeModel.setRoot() to remove the root
14.67.15.Searching node in a JTree