- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreePanel.java
More file actions
Latest commit
80 lines (62 loc) · 2.39 KB
/
Copy pathTreePanel.java
File metadata and controls
80 lines (62 loc) · 2.39 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.Dimension;
importjavax.swing.BorderFactory;
importjavax.swing.JPanel;
importjavax.swing.JScrollPane;
importjavax.swing.JTree;
importjavax.swing.border.Border;
importjavax.swing.tree.DefaultMutableTreeNode;
importjavax.swing.tree.TreeSelectionModel;
publicclassTreePanelimplementsGUI {
privatestaticTreePanelpanel;
privateJTreetree;
privateDefaultMutableTreeNoderootNode;
privateJPaneltreePanel;
//singelton so only one tree can exist
publicstaticTreePanelpanel() {
if (panel == null) {
panel = newTreePanel();
}
returnpanel;
}
publicTreePanel() {
}
//intialize
publicvoidinitialize() {
this.rootNode = newDefaultMutableTreeNode("Root Node");
this.tree = newJTree(rootNode);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
this.treePanel = newJPanel();
formatPanel();
}
//format the panel
publicvoidformatPanel() {
BordertreeBorder = BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.RED), "Tree View");
treePanel.setBorder(treeBorder);
treePanel.setPreferredSize(newDimension(200, 400));
treePanel.setLayout(newBorderLayout());
treePanel.add(newJScrollPane(tree));
}
//retrieves the seleceted node in a tree
publicDefaultMutableTreeNodegetSelectedNode() {
return (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
}
//adds a group to the tree
publicvoidaddGroupToTree(StringgroupName, DefaultMutableTreeNodeparentNode) {
DefaultMutableTreeNodegroupNode = newDefaultMutableTreeNode(groupName);
parentNode.add(groupNode);
tree.updateUI(); // Refresh the tree model to reflect the changes
}
//adds a user to the tree
publicvoidaddUserToTree(StringuserName, DefaultMutableTreeNodeparentNode) {
DefaultMutableTreeNodeuserNode = newDefaultMutableTreeNode(userName);
parentNode.add(userNode);
tree.updateUI(); // Refresh the tree model to reflect the changes
}
//renders the tree
publicJPanelrender() {
initialize();
returnthis.treePanel;
}
}