The example Java program below shows how to create a tree structure using the JTree class. The tree is created by building up a structure using DefaultMutableTreeNodes. It's placed in a JScrollPane and into a JFrame along with a JTextArea to create a simple GUI.
The JTree implements the TreeExpansionListener as an anonymous inner class. When the tree expansion events occur - when a node is expanded or collapsed - the event information is displayed using the JTextArea.
The article that goes with this Java code listing is How to Implement a TreeExpansionListener.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.JScrollPane;
import javax.swing.event.TreeExpansionListener;
import javax.swing.event.TreeExpansionEvent;
public class TreeExpansionListenerExample {
JTree exTree;
JTextArea feedbackArea;
//Note: Typically the main method will be in a
//separate class. As this is a simple one class
//example it's all in the one class.
public static void main(String[] args) {
//Use the event dispatch thread for Swing components
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new TreeExpansionListenerExample();
}
});
}
public TreeExpansionListenerExample()
{
JFrame guiFrame = new JFrame();
//make sure the program exits when the frame closes
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Creating a Table Example");
guiFrame.setSize(700,500);
//This will center the JFrame in the middle of the screen
guiFrame.setLocationRelativeTo(null);
//Create the tree stucture using the DefaultMutableTreeNodes
//The tree is a representation of a small part of the British Royal Family
DefaultMutableTreeNode Royals = new DefaultMutableTreeNode("Queen Elizabeth - Prince Philip");
DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Prince Charles");
Royals.add(child1);
DefaultMutableTreeNode grandChild1 = new DefaultMutableTreeNode("Prince Harry");
DefaultMutableTreeNode grandChild2 = new DefaultMutableTreeNode("Prince William");
child1.add(grandChild1);
child1.add(grandChild2);
DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Princess Anne");
Royals.add(child2);
DefaultMutableTreeNode grandChild3 = new DefaultMutableTreeNode("Peter Philips");
child2.add(grandChild3);
DefaultMutableTreeNode greatgrandChild1 = new DefaultMutableTreeNode("Savannah Philips");
grandChild3.add(greatgrandChild1);
DefaultMutableTreeNode greatgrandChild2 = new DefaultMutableTreeNode("Isla Philips");
grandChild3.add(greatgrandChild2);
DefaultMutableTreeNode grandChild4 = new DefaultMutableTreeNode("Zara Philips");
child2.add(grandChild4);
DefaultMutableTreeNode child3 = new DefaultMutableTreeNode("Prince Andrew");
Royals.add(child3);
DefaultMutableTreeNode grandChild5 = new DefaultMutableTreeNode("Princess Beatrice");
child3.add(grandChild5);
DefaultMutableTreeNode grandChild6 = new DefaultMutableTreeNode("Princess Eugenie");
child3.add(grandChild6);
DefaultMutableTreeNode child4 = new DefaultMutableTreeNode("Prince Edward");
Royals.add(child4);
DefaultMutableTreeNode grandChild7 = new DefaultMutableTreeNode("Lady Louise");
child4.add(grandChild7);
DefaultMutableTreeNode grandChild8 = new DefaultMutableTreeNode("Viscount Severn");
child4.add(grandChild8);
exTree = new JTree(Royals);
JScrollPane treePane = new JScrollPane(exTree);
//Add the TreeExpansionListener as an anonymous inner class.
//The treecollapsed and treeExpanded just appened information
//to the JTextArea
exTree.addTreeExpansionListener(new TreeExpansionListener(){
@Override
public void treeCollapsed(TreeExpansionEvent e)
{
feedbackArea.append("Node collapsed at " + e.getPath() + "\n");
}
@Override
public void treeExpanded(TreeExpansionEvent e)
{
feedbackArea.append("Node expanded at " + e.getPath()+ "\n");
}
});
feedbackArea = new JTextArea("Event Feedback..\n");
JScrollPane textPane = new JScrollPane(feedbackArea);
guiFrame.add(treePane, BorderLayout.NORTH);
guiFrame.add(textPane, BorderLayout.CENTER);
guiFrame.setVisible(true);
}
}

