Y04.java
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.*; // for TreeSelectionListener
import java.util.*; // for Enumeration
import java.awt.*;
import java.awt.event.*;
/**
Simple JTree demonstration.
Includes a listener to trap tree selection events.
*/
public class Y04
{
public static void main(String argv[])
{
JFrame f = new QFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.show();
}
}
class QFrame extends JFrame
{
public QFrame()
{
setTitle("CISC370-011 -- JTree demo");
setSize(300,300);
setLocation(100,100);
Container cp = getContentPane();
// Our sample tree is built using the default tree model.
root = makeSampleTree();
model = new DefaultTreeModel(root);
tree = new JTree(model);
tree.setEditable(true);
JScrollPane sp = new JScrollPane(tree);
cp.add(sp,BorderLayout.CENTER);
// A panel to contain control buttons.
JPanel buttonPanel = new JPanel();
// A button to delete the first of the currently-selected
// nodes, along with all its children.
JButton delButton = new JButton("Delete");
buttonPanel.add(delButton);
delButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
DefaultMutableTreeNode selNode =
(DefaultMutableTreeNode)tree.
getLastSelectedPathComponent();
if (selNode == null ||
selNode.getParent() == null)
return;
model.removeNodeFromParent(selNode);
}
});
// A button to enumerate (on System.out) the tree
// starting at the first currently-selected node.
JButton enumButton = new JButton("Enumerate");
buttonPanel.add(enumButton);
enumButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
DefaultMutableTreeNode selNode =
(DefaultMutableTreeNode)tree.
getLastSelectedPathComponent();
if (selNode == null)
return;
Enumeration pot =
((DefaultMutableTreeNode)selNode).
preorderEnumeration();
// Here, we compute the indentation level of
// root of the selected node. We'll indent
// other nodes relative to this level.
TreeNode[] x = model.getPathToRoot(selNode);
int level = x.length;
System.out.println();
System.out.println("Enumerating...");
while (pot.hasMoreElements()) {
TreeNode tn = (TreeNode)pot.nextElement();
// The next few lines are an inefficient
// way to indent each node. In real code,
// we'd never rebuild a path back to the
// root for each node we print out.
x = model.getPathToRoot(tn);
for (int k=0; k<x.length-level; k++)
System.out.print(" ");
System.out.println(tn);
}
System.out.println();
}
});
cp.add(buttonPanel,BorderLayout.SOUTH);
// A listener that prints (to System.out) whenever the
// selection changes. Note that multiple selection is
// supported.
tree.addTreeSelectionListener(
new TreeSelectionListener()
{
public void valueChanged(TreeSelectionEvent e)
{
TreePath[] tp = tree.getSelectionPaths();
if (tp == null)
return;
System.out.println();
System.out.println("Tree selection(s):");
for (int i=0; i<tp.length; i++)
System.out.println(" " + tp[i]);
}
});
}
// Hard-coded data for a sample tree.
private TreeNode makeSampleTree()
{
DefaultMutableTreeNode root,country,state,city;
root = new DefaultMutableTreeNode("World");
country = new DefaultMutableTreeNode("USA");
root.add(country);
state = new DefaultMutableTreeNode("Delaware");
country.add(state);
city = new DefaultMutableTreeNode("Wilmington");
state.add(city);
city = new DefaultMutableTreeNode("Newark");
state.add(city);
city = new DefaultMutableTreeNode("Dover");
state.add(city);
state = new DefaultMutableTreeNode("Maryland");
country.add(state);
city = new DefaultMutableTreeNode("Annapolis");
state.add(city);
city = new DefaultMutableTreeNode("Baltimore");
state.add(city);
state = new DefaultMutableTreeNode("Virginia");
country.add(state);
city = new DefaultMutableTreeNode("Richmond");
state.add(city);
city = new DefaultMutableTreeNode("Roanoke");
state.add(city);
state = new DefaultMutableTreeNode("Pennsylvania");
country.add(state);
city = new DefaultMutableTreeNode("Pittsburgh");
state.add(city);
city = new DefaultMutableTreeNode("Harrisburg");
state.add(city);
city = new DefaultMutableTreeNode("Philadelphia");
state.add(city);
state = new DefaultMutableTreeNode("New Jersey");
country.add(state);
city = new DefaultMutableTreeNode("Trenton");
state.add(city);
city = new DefaultMutableTreeNode("Cherry Hill");
state.add(city);
city = new DefaultMutableTreeNode("Newark");
state.add(city);
city = new DefaultMutableTreeNode("Piscataway");
state.add(city);
city = new DefaultMutableTreeNode("New Brunswick");
state.add(city);
return root;
}
private JTree tree;
private TreeNode root;
private DefaultTreeModel model;
}