看到很多人問關于用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

??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

210

211

212

213

214

215

216

217

218

219

220

221



222

223

224

225

226

227

228

229

230

231

232

233



234

235

236

237

238

239

240

241

242



243

244

245

246

247

248



249

250

251

252

253

254

255



256

257

258

259

260

261

262



263

264

265

266

267



268

269

270

271

272



273

274

275

276

277



278

279

280

281

282

283



284

285

286

287



288

289

290

291

292



293

294

295

296

297



298

299

300

301

302

303



304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321



322

323

324

325

326

327

328

329

330



331

332

333



334

335

336

337

338

339

340

341

342

343

344



345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361



362

363

364

365

366



367

368

369

370

371

372

373

374



375

376

377

378

379

380



381

382

383

384



385

386

387

388



389

390

391

392

393

394

395

396

397
