import javax.swing.*; import javax.swing.tree.*; class TreeTest extends JFrame { TreeTest () { setSize(400,400); // The node tree is build from objects of DefaultMutableTreeNode DefaultMutableTreeNode root = new DefaultMutableTreeNode("World"); DefaultMutableTreeNode africa = new DefaultMutableTreeNode("Africa"); DefaultMutableTreeNode usa = new DefaultMutableTreeNode("USA"); root.add(africa); root.add(usa); usa.add(new DefaultMutableTreeNode("New York")); usa.add(new DefaultMutableTreeNode("Oregon")); usa.add(new DefaultMutableTreeNode("Florida")); africa.add(new DefaultMutableTreeNode("Botswana")); africa.add(new DefaultMutableTreeNode("Gambia")); DefaultMutableTreeNode tanzania = new DefaultMutableTreeNode("Tanzania"); africa.add(tanzania); tanzania.add(new DefaultMutableTreeNode("Kilimanjaro")); tanzania.add(new DefaultMutableTreeNode("Dodoma")); tanzania.add(new DefaultMutableTreeNode("Dar es-Salaam")); DefaultTreeModel model = new DefaultTreeModel(root); // model needs a tree of nodes JTree tree = new JTree(model); // JTree needs a model add(tree); // add to frame setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] u) { new TreeTest(); } }