Step 5


Overview

In this step you will add a tool bar to the application. The tool bar contains buttons that duplicate the actions of the "Open...", "Save As ...", and "Exit" menu items. Each button displays an icon for the action. When the cursor is moved over one of the buttons a short description of what it does (a tool tip) pops up.

All of the changes for this step are made in the FileViewer class. There, you will create a toolbar, add your "Open...", Sava As...", and "Exit" actions to it, and install the toolbar into the application. By setting some more action properties, you can put icons onto the toolbar buttons and supply tooltip text.

Creating and Installing the Toolbar

At the end of the FileViewer constructor, you should create a local variable of type JToolBar. It should be initialized by invoking the JToolBar constructor, which has no parameters. Then send three add() messages to the toolbar, adding each of the action objects that you used in the menus. Finally, add the toolbar to the NORTH region of the content pane.

Adding Icons and Tooltip Text to the Toolbar Buttons

Adding icons and tooltip text to toolbar buttons is a simple matter of setting properties of the actions, similar to setting the name used in menu. You will need to add two statements to do this at the end of each block of code that you have for the actions. Here are the statements for the "Exit" action.
    exitAction.put(Action.SMALL_ICON, new ImageIcon("Exit24.gif"));
    exitAction.put(Action.SHORT_DESCRIPTION, "Exit the program");

For the "Open..." action, you should specify the file "Open24.gif" and use "Display text from a file" as the short description. For the "Save As..." action, you should specify the file "Save24.gif" and use "Save text to a file" as the short description.

Thats it for the coding. Now its time to try it out. Recompile the FileViewer class an run the application. You should see a new toolbar with three buttons. They should display icons instead of a name, but otherwise they should work like the menu items. If you are not sure which button is which, look at the "File" menu. It should display the icons along with the name.

When you move the cursor over one of the buttons, after a second or two, the tooltip text should pop up. If all goes well, you are done, done, done.