/* Demonstrate a tree events
*/
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
class TreeEventDemo
{
JLabel jlab;
TreeEventDemo()
{
// Create a new JFrame container
JFrame jfrm = new JFrame("Tree Event Demo");
// Use the default border layout manager
// give the frame an initial size
jfrm.setSize(200, 200);
// terminate program when the user closes the application
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create a label that will display the tree selection
jlab = new JLabel();
/* Begin creating the tree be defining the
* structure and realtionship of its nodes*/
// First, create the root node of the ttree
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Food");
/* Next, create twoo subtrees. One contains fruit.
* The other vegetables.*/
// Create the root of the Fruit subtree
DefaultMutableTreeNode fruit = new DefaultMutableTreeNode("Fruit");
root.add(fruit); // add the Fruit node to the tree
/* The Fruit subtree has two subtrees on its own.
* The first is Apples, the second is Pears.*/
// Create an Apples subtree
DefaultMutableTreeNode apples = new DefaultMutableTreeNode("Apples");
fruit.add(apples);
/* Populate the Apples subtree by adding
* apple varieties to the Apples subtree*/
apples.add(new DefaultMutableTreeNode("Jonathan"));
apples.add(new DefaultMutableTreeNode("Winesap"));
//Create a Pears subtree
DefaultMutableTreeNode pears = new DefaultMutableTreeNode("Pears");
fruit.add(pears);
// Populate the Pears subtree
pears.add(new DefaultMutableTreeNode("Barlett"));
// Create the root of the Vegetables subtree
DefaultMutableTreeNode veg = new DefaultMutableTreeNode("Vegetables");
root.add(veg);
// Populate Vegetables
veg.add(new DefaultMutableTreeNode("Beans"));
veg.add(new DefaultMutableTreeNode("Corn"));
veg.add(new DefaultMutableTreeNode("Potatoes"));
veg.add(new DefaultMutableTreeNode("Rice"));
/* Now, create a JTree that uses the structure
* defined by the preceding statements*/
JTree jtree = new JTree(root);
// Allow the tree to be edited so that model events can be generated
jtree.setEditable(true);
//Set the tree selection mode to single selection
TreeSelectionModel tsm = jtree.getSelectionModel();
tsm.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
// Finally, wrap the tree in scroll pane
JScrollPane jscrlp = new JScrollPane(jtree);
// Listen for tree expansion events
jtree.addTreeExpansionListener(new TreeExpansionListener()
{
public void treeExpanded(TreeExpansionEvent tse)
{
// Get the path to the expansion point
TreePath tp = tse.getPath();
// Display the node
jlab.setText("Expansion: " + tp.getLastPathComponent() );
}
public void treeCollapsed(TreeExpansionEvent tse)
{
// Get the path to the expansion point
TreePath tp = tse.getPath();
// Display the node
jlab.setText("Collapse: " + tp.getLastPathComponent() );
}
});
// Listen for tree selection events
jtree.addTreeSelectionListener(new TreeSelectionListener()
{
public void valueChanged(TreeSelectionEvent tse)
{
// Get the path to the selection
TreePath tp = tse.getPath();
//Display the selected node
jlab.setText("Selection event: " +tp.getLastPathComponent() );
}
});
// Listen for tree model events.Notice that the listener is registered with tree model
jtree.getModel().addTreeModelListener(new TreeModelListener()
{
public void treeNodesChanged(TreeModelEvent tse)
{
// Get the path to the change
TreePath tp = tse.getTreePath();
// Display the Path
jlab.setText("Model change path: " + tp);
}
/* Empty implementations of the remaining TreeModelEvent
* methods. Implement these if your application
* needs to handle these actions*/
public void treeNodesInserted(TreeModelEvent tse){}
public void treeNodesRemoved(TreeModelEvent tse){}
public void treeStructureChanged(TreeModelEvent tse){}
});
// Add the tree and label to the content pane
jfrm.getContentPane().add(jscrlp, BorderLayout.CENTER);
jfrm.getContentPane().add(jlab, BorderLayout.SOUTH);
// Display the frame
jfrm.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new TreeEventDemo();
}
});
}
}