
154
|
Chapter 4, File Choosers
#29 Display Shortcuts in the JFileChooser
HACK
H A C K
#29
Display Shortcuts in the JFileChooser
Hack #29
This hack will customize the JFileChooser to recognize shortcut (linked)
folders and overlay them with a link graphic, mimicking the native Windows
File Explorer.
Another of JFileChooser’s glaring bugs is the lack of any support for linked
directories. This is hardly surprising, as Java itself has no understanding of
linked directories. Most operating systems support linked files, however,
and often indicate to the user that a file is linked—for example, by drawing
an arrow overlaid on top of the folder or directory icon. Compare the typi-
cal
JFileChooser in Figure 4-2 to the standard Windows file chooser in
Figure 4-3. There’s more than a small difference! No wonder it’s hard to get
folks to move to Swing.
Like every Swing component, the look of the
JFileChooser is controlled by
the installed Look and Feel (L&F). However, the
JFileChooser also uses a
custom class similar to a table cell renderer for drawing the actual files and
folders. That class,
FileView, is the best place to start hacking the
JFileChooser’s display.
The
FileView contains five methods that determine the names, icons, and
other attributes that are actually displayed in a
JFileChooser. By overriding
these methods, you can change the look or text of any file. To draw shortcuts
as linked folders, you just ne ...