More action with Struts 2
In a recent review of Struts 2 in Action, JW Blogger Oleg Mikheev notes that Struts 2 is "just a collection of extensions built upon WebWork, which is ultimately the right thing to learn before starting a Struts 2 project." While Struts 2 has some architectural flaws, Oleg calls WebWork well-designed, well-tested, and reliable. What are your experiences using Struts 2 and WebWork?

Also see "Hello World the WebWork way," a JavaWorld excerpt from WebWork in Action, by Patrick Lightbody and Jason Carreira.

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Java Tip 119: Don't know much about file history?

Enhance your file processing application by adding a file history mechanism

Every programmer is familiar with a text editor either as part of an IDE or as a standalone tool. Often you must reedit source code you have recently closed. Rather than use the file chooser by first selecting the Open file menu (a sometimes time-consuming process), you can just click on the file menu and get a list of your last edited files. This list is typically enumerated and sorted with the most recently processed filename at the top.

My "Java Tip 100: Add a History Mechanism to JFileChooser" enhanced the JFileChooser component itself by providing a directory history as well as a file previewer in the form of a JFileChooser accessory. Now we will enhance the File menu in the JFrame's menubar by providing a history mechanism for filenames.

In this tip, we'll develop three classes to enhance a sample application with such a file history mechanism. The DemoFileHistory class is a short GUI-based demo application. (Note that this demo uses a JButton feature that requires JDK 1.3 or later to compile and run.) The FileHistory class encapsulates all the code you need to create the file history mechanism, maintain the file list, and save and restore the list after the application has been closed or reopened, respectively. The third class is the IFileHistory Java interface, which is defined inside the FileHistory class. IFileHistory encapsulates the five methods that your GUI frame must implement in order to satisfy the mechanism's needs.

Develop the demo application

Let's start by developing a small demo application. This application extends Swing's JFrame. It has a menubar and toolbar at the top, a label canvas in the center, and a status bar at the bottom. Figure 1 illustrates this application.

Figure 1. The demo application with File and Options menus. Click on thumbnail to view full-size image.

The menubar contains two menus: File and Options. The File menu has a New item that clears the main screen area. The New item's activation is reported in the status line. We will enhance the File menu's Open file item with our file history mechanism. We have corresponding toolbar icons for New and Open. Menu items and toolbar actions are connected to Swing action objects. If you look at the source code, so far this is classic Swing application development.

The user maintains the filename history list using the Options menu. For our demo, the Options menu has only one item called File History, which you activate to get your current file history list. (See Figure 2 below.) By clicking a list item (i.e. a filename), you can select the item to open or delete it from the list with the Delete button shown in Figure 2. Using the Shift key, you can select a contiguous item range; using the CTRL key, you can select a noncontiguous item range -- as done in other applications.

Figure 2. The File History maintenance window. Click on thumbnail to view full-size image.

If you open a file with the Open menu or the Open icon you get a JFileChooser that asks you to select your file. After selecting the filename (or an abbreviated form), the filename is displayed and mirrored in the center of the application frame. The status line reflects the pathname, simulating a file processing activity. If you open three different files and you click on the File menu, you get a list as shown in Figure 3.

Resources