Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Subscription to Java Pro

email article
printer friendly
get the code
more resources

Whipping Up GUI Components
Apply a text-based resource bundle to create menu bars and toolbars. Part 1 introduces a framework that eliminates the tedium of creating necessary GUI interface components
by Patrick G. Durand

Posted May 5, 2004

Editor's Note: This is the first of a two-part article that discusses a process for automating the creation of menu bars and toolbars in newly developed GUI interfaces. Part 1 takes a detailed look at the resource bundle (text file) that supplies the information necessary for the Java API's ResourceBundle framework to create Swing components. Part 2 will take a look at using the framework's DDActionManager and DDBasicAction classes.

Typically we provide a menu bar and toolbars when we are developing a graphical user interface. Even if the Swing library provides all the required components for this task, the process of writing the Java code to handle menu bar and toolbar actions is repetitive, tedious, and, honestly, not much fun. To create and handle menus and toolbars easily, we have designed a system that is able to create these tools using just a text file—a resource bundle. This file contains a text description of the various actions, and the system is capable of using it to create all the Swing components (Action, JMenuBar, and JToolBar) as well as provide a generic framework to handle the action events. In addition to the resource bundle, you need only to provide the core code of each action, since this part is highly specific to each application.

Almost all software having a graphical user interface provides a menu as a standard and easy way to let the user interact with the application. Often, this menu is provided as the well-known menu bar located just below the main frame's title bar. Very often the actions available from the menu bar are duplicated in a second graphic component, a toolbar, as well.

The Swing library provides all the required components to create menus and toolbars. Most important among them are JMenuBar, JMenu, and JMenuItem to create a menu bar, and JToolBar to create a toolbar (all these components are part of the javax.swing package). Detailing their use is beyond the scope of this discussion and we assume that you're familiar with them. (However, if you need more information about using Swing library components, see Resources.)

Since the goal of a menu or a toolbar is to provide a graphical access to the application functions, or actions, Swing also provides a generic action framework centered on the avax.swing.Action interface. Real application actions can then be implemented as a set of classes implementing that interface and providing the core code for the actions. For example, actions can be opening and closing a file or copying and pasting text between text components of the GUI. Objects implementing the Action interface can then be passed to menu and tool components to link the action's core code to GUI elements, namely menu items and toolbar buttons (see Resources).

The analysis of the code written to create and handle menu bars and toolbars in various applications reveals that we always write the same core code: a set of classes implementing the Action interface or better, inheriting from abstractAction class; a corresponding set of JMenuItem objects organized within one or more JMenu objects, all of them attached to a single JMenuBar object; and a corresponding set of buttons attached to a JToolBar object.

In addition to all the code, the actions, menu items, and toolbar buttons are usually configured with various decorations: icons, keyboard accelerators, and mnemonics. Instead of always providing that core code, why not use a generic framework that is able to create those GUI components and handle basic action events given a single file containing the description of the actions? The DDMenu framework provides the answer.




Back to top












Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home