Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
I also decided to provide a file previewer with the directory navigator, for two reasons: First, I thought the mechanisms
complemented one another, and second, the JFileChooser accessory layout looked much better with another panel below the navigator combo box widget. So, this Java tip discusses
how to enhance the standard Swing JFileChooser by providing a directory history and a file previewer.
Adding accessories to JFileChooser is easy, and has been described previously in Java Tip 93 for the File Finder accessory. For convenience I will combine the File Finder with my navigator/previewer in one accessory
that is controlled by a JTabbedPane. As shown in Java Tip 93, the major work is the accessory implementation, and not the linking to the JFileChooser component. Now I'll take a closer look at how your users will benefit from this implementation and how you can achieve it.
Figure 1 shows the accessory in action: I have selected the file Menu_help_texts.txt from the E:\JavaSourceExamples\Swing directory. Its first 500 bytes are shown in the preview pane below the navigator combo box. In Figure 2 the combo list is expanded so that the user can select one of three directories. The last directory name is larger than the available display area in the combo box; you have a tooltip connected to that item to show the user the complete path name.
Figure 1. The TextPreviewer in action. Click on thumbnail
to view full image (12 KB)
Figure 2. The directory history combo box with tooltips. Click on thumbnail
to view full image (12 KB)
EFileChooser is a class that enhances JFileChooser by subclassing it. EFileChooser provides all the functionality necessary to add the accessory components and to connect them to the different file chooser
events. First I'll cover the implementation of the navigator combo box, which is basically a JComboBox that has a vector list with previously visited directories as its internal model. Every item in the list is a String that
represents the directory path name. Because you will make the combo box remember its state after you restart the application,
you will implement an object serialization mechanism that writes and reads the internal model -- that is, the vector object.
Note that the directory list is not implemented as a ring buffer. It will eventually grow depending on how many different directories you are using. You can improve the code by allowing it to remember only the last 10 or so entries (the length of the ring buffer should be customizable by the user).