JTree
The JTree class displays items which are structured in a hierarchy. Each item is known as a node, and each node can contain other nodes as its "children". Like JList and JTable, the JTree uses an associated model object (a TreeModel) to provide its data. However, in many cases it is easier to make use of the convenience class DefaultMutableTreeNode to represent each node in the tree. Suppose you want to model some of the countries of the five continents and have them displayed in a tree:
// Create the root node
DefaultMutableTreeNode continents =
new DefaultMutableTreeNode("Continents");
// Create a node for Africa and add some sample countries
DefaultMutableTreeNode africa =
new DefaultMutableTreeNode("Africa");
africa.add(new DefaultMutableTreeNode("Kenya"));
africa.add(new DefaultMutableTreeNode("Nigeria"));
// Create a node for America and add some sample countries
DefaultMutableTreeNode america =
new DefaultMutableTreeNode("America");
america.add(new DefaultMutableTreeNode("Canada"));
america.add(new DefaultMutableTreeNode("USA"));
// Create a node for Asia and add some sample countries
DefaultMutableTreeNode asia =
new DefaultMutableTreeNode("Asia");
asia.add(new DefaultMutableTreeNode("China"));
asia.add(new DefaultMutableTreeNode("Japan"));
// Create a node for Australia and add some sample countries
DefaultMutableTreeNode australia =
new DefaultMutableTreeNode("Australia");
australia.add(new DefaultMutableTreeNode("Australia"));
// Create a node for Europe and add some sample countries
DefaultMutableTreeNode europe =
new DefaultMutableTreeNode("Europe");
europe.add(new DefaultMutableTreeNode("France"));
europe.add(new DefaultMutableTreeNode("Germany"));
DefaultMutableTreeNode uk =
new DefaultMutableTreeNode("UK");
uk.add(new DefaultMutableTreeNode("England"));
uk.add(new DefaultMutableTreeNode("Northern Ireland"));
uk.add(new DefaultMutableTreeNode("Scotland"));
uk.add(new DefaultMutableTreeNode("Wales"));
europe.add(uk);
// Add the continents to the root node
continents.add(africa);
continents.add(america);
continents.add(asia);
continents.add(australia);
continents.add(europe);
// Create a JTree with the root node
JTree tree = new JTree(continents);
// Add the tree to the panel
panel.add(new JScrollPane(tree));
- Each item in the tree is an instance of
DefaultMutableTreeNode - One of these instances must be the root node, being the one at the top-level that gets assigned to the tree. Here, it is continents
- To each node you can add other nodes as its children, and therefore the node that contains one or more children is known as its parent
- The
JTreeshould be nested inside aJScrollPane
Run the application to see the following display:
You can expand or contract any node by clicking the small "handle" graphic to the left of any parent item. If you expand Europe and then UK the screen should look as follows:
- Note that you might get scrollbars appear when you expand the nodes: the above screen shot has adjusted the size of the window so that any scrollbars disappear
Comments