Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Java Tip 100: Add a history mechanism to JFileChooser

Enhance JFileChooser by implementing a directory history and file previewer accessory

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Good user interface design tries to minimize the needed user input. For example, some Unix system shells and editors provide auto completion for command input. The Windows desktop maintains a list of documents previously opened. And Netscape Navigator and Microsoft Internet Explorer have a URL history combo box or a list to track visited Websites. In the Java project I'm currently working on, my team and I built a test-and-monitoring tool to observe and analyze distributed systems. The user organizes his input data -- for example, trace files -- in different directories. Inspired by the history mechanisms in other systems, I had the idea to implement a history feature for directories as a reusable Java component.

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)



Using EFileChooser as a wrapper

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).

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources