看到很多人问关于用JTree实现资源管理器的方法,在这里我提供一个例子共大家参考,这个例子虽然也是转自其他书,但是JTree的用法,我掌握的差不多了,如果哪位朋友,对此例子有问题,我愿意与他交流。我的email是:jack_kui@126.com这个例子涵盖了JTree的大部分属性,希望对大家能够有所帮助。需要一些图标文件,请大家自己复制好相同名字的gif图片放到目录下。
下面是源代码:
1
import
java.awt.
*
;
2
import
java.awt.event.
*
;
3
import
java.io.
*
;
4
import
java.util.
*
;
5
6
import
javax.swing.
*
;
7
import
javax.swing.tree.
*
;
8
import
javax.swing.event.
*
;
9
10
public
class
FileTree1
11
extends
JFrame
12
{
13
public
static
final
ImageIcon ICON_COMPUTER
=
14
new
ImageIcon(
"
computer.gif
"
);
15
public
static
final
ImageIcon ICON_DISK
=
16
new
ImageIcon(
"
disk.gif
"
);
17
public
static
final
ImageIcon ICON_FOLDER
=
18
new
ImageIcon(
"
folder.gif
"
);
19
public
static
final
ImageIcon ICON_EXPANDEDFOLDER
=
20
new
ImageIcon(
"
expandedfolder.gif
"
);
21
22
protected
JTree m_tree;
23
protected
DefaultTreeModel m_model;
24
protected
JTextField m_display;
25
26
public
FileTree1()
27
{
28
super
(
"
Directories Tree
"
);
29
setSize(
400
,
300
);
30
31
DefaultMutableTreeNode top
=
new
DefaultMutableTreeNode(
32
new
IconData(ICON_COMPUTER,
null
,
"
Computer
"
));
33
34
DefaultMutableTreeNode node;
35
File[] roots
=
File.listRoots();
36
for
(
int
k
=
0
; k
<
roots.length; k
++
)
37
{
38
node
=
new
DefaultMutableTreeNode(
new
IconData(ICON_DISK,
39
null
,
new
FileNode(roots[k])));
40
top.add(node);
41
node.add(
new
DefaultMutableTreeNode(
new
Boolean(
true
)));
42
}
43
44
m_model
=
new
DefaultTreeModel(top);
45
m_tree
=
new
JTree(m_model);
46
47
m_tree.putClientProperty(
"
JTree.lineStyle
"
,
"
Angled
"
);
48
49
TreeCellRenderer renderer
=
new
50
IconCellRenderer();
51
m_tree.setCellRenderer(renderer);
52
53
m_tree.addTreeExpansionListener(
new
54
DirExpansionListener());
55
56
m_tree.addTreeSelectionListener(
new
57
DirSelectionListener());
58
59
m_tree.getSelectionModel().setSelectionMode(
60
TreeSelectionModel.SINGLE_TREE_SELECTION);
61
m_tree.setShowsRootHandles(
true
);
62
m_tree.setEditable(
false
);
63
64
JScrollPane s
=
new
JScrollPane();
65
s.getViewport().add(m_tree);
66
getContentPane().add(s, BorderLayout.CENTER);
67
68
m_display
=
new
JTextField();
69
m_display.setEditable(
false
);
70
getContentPane().add(m_display, BorderLayout.NORTH);
71
72
WindowListener wndCloser
=
new
WindowAdapter()
73
{
74
public
void
windowClosing(WindowEvent e)
75
{
76
System.exit(
0
);
77
}
78
}
;
79
addWindowListener(wndCloser);
80
81
setVisible(
true
);
82
}
83
84
DefaultMutableTreeNode getTreeNode(TreePath path)
85
{
86
return
(DefaultMutableTreeNode)(path.getLastPathComponent());
87
}
88
89
FileNode getFileNode(DefaultMutableTreeNode node)
90
{
91
if
(node
==
null
)
92
return
null
;
93
Object obj
=
node.getUserObject();
94
if
(obj
instanceof
IconData)
95
obj
=
((IconData)obj).getObject();
96
if
(obj
instanceof
FileNode)
97
return
(FileNode)obj;
98
else
99
return
null
;
100
}
101
102
//
Make sure expansion is threaded and updating the tree model
103
//
only occurs within the event dispatching thread.
104
class
DirExpansionListener
implements
TreeExpansionListener
105
{
106
public
void
treeExpanded(TreeExpansionEvent event)
107
{
108
final
DefaultMutableTreeNode node
=
getTreeNode(
109
event.getPath());
110
final
FileNode fnode
=
getFileNode(node);
111
112
Thread runner
=
new
Thread()
113
{
114
public
void
run()
115
{
116
if
(fnode
!=
null
&&
fnode.expand(node))
117
{
118
Runnable runnable
=
new
Runnable()
119
{
120
public
void
run()
121
{
122
m_model.reload(node);
123
}
124
}
;
125
SwingUtilities.invokeLater(runnable);
126
}
127
}
128
}
;
129
runner.start();
130
}
131
132
public
void
treeCollapsed(TreeExpansionEvent event)
{}
133
}
134
135
136
class
DirSelectionListener
137
implements
TreeSelectionListener
138
{
139
public
void
valueChanged(TreeSelectionEvent event)
140
{
141
DefaultMutableTreeNode node
=
getTreeNode(
142
event.getPath());
143
FileNode fnode
=
getFileNode(node);
144
if
(fnode
!=
null
)
145
m_display.setText(fnode.getFile().
146
getAbsolutePath());
147
else
148
m_display.setText(
""
);
149
}
150
}
151
152
public
static
void
main(String argv[])
153
{
154
new
FileTree1();
155
}
156
}
157
158
class
IconCellRenderer
159
extends
JLabel
160
implements
TreeCellRenderer
161
{
162
protected
Color m_textSelectionColor;
163
protected
Color m_textNonSelectionColor;
164
protected
Color m_bkSelectionColor;
165
protected
Color m_bkNonSelectionColor;
166
protected
Color m_borderSelectionColor;
167
168
protected
boolean
m_selected;
169
170
public
IconCellRenderer()
171
{
172
super
();
173
m_textSelectionColor
=
UIManager.getColor(
174
"
Tree.selectionForeground
"
);
175
m_textNonSelectionColor
=
UIManager.getColor(
176
"
Tree.textForeground
"
);
177
m_bkSelectionColor
=
UIManager.getColor(
178
"
Tree.selectionBackground
"
);
179
m_bkNonSelectionColor
=
UIManager.getColor(
180
"
Tree.textBackground
"
);
181
m_borderSelectionColor
=
UIManager.getColor(
182
"
Tree.selectionBorderColor
"
);
183
setOpaque(
false
);
184
}
185
186
public
Component getTreeCellRendererComponent(JTree tree,
187
Object value,
boolean
sel,
boolean
expanded,
boolean
leaf,
188
int
row,
boolean
hasFocus)
189
190
{
191
DefaultMutableTreeNode node
=
192
(DefaultMutableTreeNode)value;
193
Object obj
=
node.getUserObject();
194
setText(obj.toString());
195
196
if
(obj
instanceof
Boolean)
197
setText(
"
Retrieving data
"
);
198
199
if
(obj
instanceof
IconData)
200
{
201
IconData idata
=
(IconData)obj;
202
if
(expanded)
203
setIcon(idata.getExpandedIcon());
204
else
205
setIcon(idata.getIcon());
206
}
207
else
208
setIcon(
null
);
209
210
setFont(tree.getFont());
211
setForeground(sel
?
m_textSelectionColor :
212
m_textNonSelectionColor);
213
setBackground(sel
?
m_bkSelectionColor :
214
m_bkNonSelectionColor);
215
m_selected
=
sel;
216
return
this
;
217
}
218
219
220
public
void
paintComponent(Graphics g)
221
{
222
Color bColor
=
getBackground();
223
Icon icon
=
getIcon();
224
225
g.setColor(bColor);
226
int
offset
=
0
;
227
if
(icon
!=
null
&&
getText()
!=
null
)
228
offset
=
(icon.getIconWidth()
+
getIconTextGap());
229
g.fillRect(offset,
0
, getWidth()
-
1
-
offset,
230
getHeight()
-
1
);
231
232
if
(m_selected)
233
{
234
g.setColor(m_borderSelectionColor);
235
g.drawRect(offset,
0
, getWidth()
-
1
-
offset, getHeight()
-
1
);
236
}
237
super
.paintComponent(g);
238
}
239
}
240
241
class
IconData
242
{
243
protected
Icon m_icon;
244
protected
Icon m_expandedIcon;
245
protected
Object m_data;
246
247
public
IconData(Icon icon, Object data)
248
{
249
m_icon
=
icon;
250
m_expandedIcon
=
null
;
251
m_data
=
data;
252
}
253
254
public
IconData(Icon icon, Icon expandedIcon, Object data)
255
{
256
m_icon
=
icon;
257
m_expandedIcon
=
expandedIcon;
258
m_data
=
data;
259
}
260
261
public
Icon getIcon()
262
{
263
return
m_icon;
264
}
265
266
public
Icon getExpandedIcon()
267
{
268
return
m_expandedIcon
!=
null
?
m_expandedIcon : m_icon;
269
}
270
271
public
Object getObject()
272
{
273
return
m_data;
274
}
275
276
public
String toString()
277
{
278
return
m_data.toString();
279
}
280
}
281
282
class
FileNode
283
{
284
protected
File m_file;
285
286
public
FileNode(File file)
287
{
288
m_file
=
file;
289
}
290
291
public
File getFile()
292
{
293
return
m_file;
294
}
295
296
public
String toString()
297
{
298
return
m_file.getName().length()
>
0
?
m_file.getName() :
299
m_file.getPath();
300
}
301
302
public
boolean
expand(DefaultMutableTreeNode parent)
303
{
304
DefaultMutableTreeNode flag
=
305
(DefaultMutableTreeNode)parent.getFirstChild();
306
if
(flag
==
null
)
//
No flag
307
return
false
;
308
Object obj
=
flag.getUserObject();
309
if
(
!
(obj
instanceof
Boolean))
310
return
false
;
//
Already expanded
311
312
parent.removeAllChildren();
//
Remove Flag
313
314
File[] files
=
listFiles();
315
if
(files
==
null
)
316
return
true
;
317
318
Vector v
=
new
Vector();
319
320
for
(
int
k
=
0
; k
<
files.length; k
++
)
321
{
322
File f
=
files[k];
323
if
(
!
(f.isDirectory()))
324
continue
;
325
326
FileNode newNode
=
new
FileNode(f);
327
328
boolean
isAdded
=
false
;
329
for
(
int
i
=
0
; i
<
v.size(); i
++
)
330
{
331
FileNode nd
=
(FileNode)v.elementAt(i);
332
if
(newNode.compareTo(nd)
<
0
)
333
{
334
v.insertElementAt(newNode, i);
335
isAdded
=
true
;
336
break
;
337
}
338
}
339
if
(
!
isAdded)
340
v.addElement(newNode);
341
}
342
343
for
(
int
i
=
0
; i
<
v.size(); i
++
)
344
{
345
FileNode nd
=
(FileNode)v.elementAt(i);
346
IconData idata
=
new
IconData(FileTree1.ICON_FOLDER,
347
FileTree1.ICON_EXPANDEDFOLDER, nd);
348
DefaultMutableTreeNode node
=
new
349
DefaultMutableTreeNode(idata);
350
parent.add(node);
351
352
if
(nd.hasSubDirs())
353
node.add(
new
DefaultMutableTreeNode(
354
new
Boolean(
true
) ));
355
}
356
357
return
true
;
358
}
359
360
public
boolean
hasSubDirs()
361
{
362
File[] files
=
listFiles();
363
if
(files
==
null
)
364
return
false
;
365
for
(
int
k
=
0
; k
<
files.length; k
++
)
366
{
367
if
(files[k].isDirectory())
368
return
true
;
369
}
370
return
false
;
371
}
372
373
public
int
compareTo(FileNode toCompare)
374
{
375
return
m_file.getName().compareToIgnoreCase(
376
toCompare.m_file.getName() );
377
}
378
379
protected
File[] listFiles()
380
{
381
if
(
!
m_file.isDirectory())
382
return
null
;
383
try
384
{
385
return
m_file.listFiles();
386
}
387
catch
(Exception ex)
388
{
389
JOptionPane.showMessageDialog(
null
,
390
"
Error reading directory
"
+
m_file.getAbsolutePath(),
391
"
Warning
"
, JOptionPane.WARNING_MESSAGE);
392
return
null
;
393
}
394
}
395
}
396
397
|
|
|
导航
统计
- 随笔: 115
- 文章: 1
- 评论: 86
- 引用: 0
常用链接
留言簿(5)
随笔档案(115)
网址
搜索
积分与排名
最新评论

阅读排行榜
评论排行榜
|
|