-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathSimpleTreeFrame.java
More file actions
45 lines (38 loc) · 1.37 KB
/
Copy pathSimpleTreeFrame.java
File metadata and controls
45 lines (38 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package tree;
import javax.swing.*;
import javax.swing.tree.*;
/**
* This frame contains a simple tree that displays a manually constructed tree model.
*/
public class SimpleTreeFrame extends JFrame
{
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;
public SimpleTreeFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
// set up tree model data
DefaultMutableTreeNode root = new DefaultMutableTreeNode("World");
DefaultMutableTreeNode country = new DefaultMutableTreeNode("USA");
root.add(country);
DefaultMutableTreeNode state = new DefaultMutableTreeNode("California");
country.add(state);
DefaultMutableTreeNode city = new DefaultMutableTreeNode("San Jose");
state.add(city);
city = new DefaultMutableTreeNode("Cupertino");
state.add(city);
state = new DefaultMutableTreeNode("Michigan");
country.add(state);
city = new DefaultMutableTreeNode("Ann Arbor");
state.add(city);
country = new DefaultMutableTreeNode("Germany");
root.add(country);
state = new DefaultMutableTreeNode("Schleswig-Holstein");
country.add(state);
city = new DefaultMutableTreeNode("Kiel");
state.add(city);
// construct tree and put it in a scroll pane
JTree tree = new JTree(root);
add(new JScrollPane(tree));
}
}