-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreePanel.java
More file actions
80 lines (62 loc) · 2.39 KB
/
TreePanel.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
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.border.Border;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
public class TreePanel implements GUI {
private static TreePanel panel;
private JTree tree;
private DefaultMutableTreeNode rootNode;
private JPanel treePanel;
//singelton so only one tree can exist
public static TreePanel panel() {
if (panel == null) {
panel = new TreePanel();
}
return panel;
}
public TreePanel() {
}
//intialize
public void initialize() {
this.rootNode = new DefaultMutableTreeNode("Root Node");
this.tree = new JTree(rootNode);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
this.treePanel = new JPanel();
formatPanel();
}
//format the panel
public void formatPanel() {
Border treeBorder = BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.RED), "Tree View");
treePanel.setBorder(treeBorder);
treePanel.setPreferredSize(new Dimension(200, 400));
treePanel.setLayout(new BorderLayout());
treePanel.add(new JScrollPane(tree));
}
//retrieves the seleceted node in a tree
public DefaultMutableTreeNode getSelectedNode() {
return (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
}
//adds a group to the tree
public void addGroupToTree(String groupName, DefaultMutableTreeNode parentNode) {
DefaultMutableTreeNode groupNode = new DefaultMutableTreeNode(groupName);
parentNode.add(groupNode);
tree.updateUI(); // Refresh the tree model to reflect the changes
}
//adds a user to the tree
public void addUserToTree(String userName, DefaultMutableTreeNode parentNode) {
DefaultMutableTreeNode userNode = new DefaultMutableTreeNode(userName);
parentNode.add(userNode);
tree.updateUI(); // Refresh the tree model to reflect the changes
}
//renders the tree
public JPanel render() {
initialize();
return this.treePanel;
}
}