forked from rick2785/JavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaLesson27.java
More file actions
209 lines (117 loc) · 5.09 KB
/
JavaLesson27.java
File metadata and controls
209 lines (117 loc) · 5.09 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import javax.swing.*;
import java.awt.event.*;
import java.awt.Dimension;
// Enumerations are used to store related items together
import java.util.Enumeration;
import javax.swing.tree.*;
public class JavaLesson27 extends JFrame{
JButton button1;
String outputString = "";
// A Tree contains nodes that can contain other nodes
JTree theTree;
// If a node holds other nodes it is called a parent node
// The nodes inside of a parent node are children nodes
// Nodes on the same level are called siblings
DefaultMutableTreeNode documents, work, games, emails;
DefaultMutableTreeNode fileSystem = new DefaultMutableTreeNode("C Drive");
public static void main(String[] args){
new JavaLesson27();
}
public JavaLesson27(){
this.setSize(400,400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("My Sixth Frame");
JPanel thePanel = new JPanel();
// Create a button
button1 = new JButton("Get Answer");
ListenForButton lForButton = new ListenForButton();
button1.addActionListener(lForButton);
thePanel.add(button1);
// Create the JTree by passing it the top tree node
theTree = new JTree(fileSystem);
// Makes sure only one item can be selected at a time
// By default you can make multiple selections
theTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
// Only show 8 rows of the tree at a time
theTree.setVisibleRowCount(8);
// Add the items to the tree documents, work, games
// We first add the documents parent node
documents = addAFile("Docs", fileSystem);
// Now we add children nodes to the documents parent node
addAFile("Taxes.exl", documents);
emails = addAFile("Emails", documents);
addAFile("Story.txt", documents);
addAFile("Schedule.txt", documents);
// Add a child node to the email node
addAFile("CallBob.txt", emails);
// Create the work node and its children
work = addAFile("Work Applications", fileSystem);
addAFile("Spreadsheet.exe", work);
addAFile("Wordprocessor.exe", work);
addAFile("Presentation.exe", work);
// Create the games node and its children
games = addAFile("Games", fileSystem);
addAFile("SpaceInvaders.exe", games);
addAFile("PacMan.exe", games);
// Put the tree in a scroll component
JScrollPane scrollBox = new JScrollPane(theTree);
// Set the size for the JScrollPane so that everything fits
Dimension d = scrollBox.getPreferredSize();
d.width = 200;
scrollBox.setPreferredSize(d);
thePanel.add(scrollBox);
this.add(thePanel);
this.setVisible(true);
}
private DefaultMutableTreeNode addAFile(String fileName, DefaultMutableTreeNode parentFolder){
// Creates a new node for the tree
DefaultMutableTreeNode newFile = new DefaultMutableTreeNode(fileName);
// Add attaches a name to the node
parentFolder.add(newFile);
// return the new node
return newFile;
}
private class ListenForButton implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource() == button1){
// How to get the selected node
// Returns the last selected node in the tree
Object treeObject = theTree.getLastSelectedPathComponent();
// Cast the Object into a DefaultMutableTreeNode
DefaultMutableTreeNode theFile = (DefaultMutableTreeNode) treeObject;
// Returns the object stored in this node and casts it to a string
String treeNode = (String) theFile.getUserObject();
outputString = "The Selected Node: " + treeNode + "\n";
// Get the number of children this node has
outputString += "Number of Children: " + theFile.getChildCount() + "\n";
// Get the number of siblings
outputString += "Number of Siblings: " + theFile.getSiblingCount() + "\n";
// Get the parent of this node
outputString += "The parent: " + theFile.getParent() + "\n";
// Get the next node
outputString += "Next Node: " + theFile.getNextNode() + "\n";
// Get the previous node
outputString += "Next Node: " + theFile.getPreviousNode() + "\n";
// Get the children for the node
outputString += "\nChildren of Node\n";
// children() returns an enumeration that contains all the children
// This for loop will continue to run as long as there are more elements
// nextElement() returns the next element in the list
for (Enumeration enumValue = theFile.children(); enumValue.hasMoreElements(); ) {
outputString += enumValue.nextElement() + "\n";
}
// Get the path from the root
outputString += "\nPath From Root\n";
// getPath returns an array of TreeNodes
TreeNode[] pathNodes = theFile.getPath();
// Cycle through the TreeNodes
for(TreeNode indivNodes: pathNodes){
outputString += indivNodes + "\n";
}
JOptionPane.showMessageDialog(JavaLesson27.this, outputString, "Information", JOptionPane.INFORMATION_MESSAGE);
outputString = "";
}
}
}
}