Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Online Training

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
 
Training Index

Magercise 6:
Using Menus, Toolbars, and Tool Tips

[Help | Solution | API Docs | Short Course | Magercises]

In this magercise, you will add a working menubar and toolbar, with tool tips, to complete a basic text editor. The editor needs menu options to start working on a new file, open an existing one, save the file, close the window, and display an about box. The toolbar you'll create will need entries for new, open, save, and about with tool tips.

Magercise 6 Prerequisites

Buttons With Icons

Skeleton Code

TextEdit.java

The skeleton code contains the functionality to do the actual work via the following methods:

  • doNewCommand()
  • doOpenCommand()
  • doSaveCommand()
  • doAboutCommand()
  • doCloseCommand(statusCode)

With the exception of doAboutCommand(), you'll just need to make the necessary connections for event handling. For doAboutCommand(), you'll need to create the necessary JDialog.

Tasks

  1. In the TextEdit constructor skeleton, setup the screen to have, from top to bottom, a JToolBar, JTextArea, and JLabel (for status messages). Save a reference to the JTextArea and JLabel in the instance variables pane and statusInfo, respectively, as the event handling methods need access to them. Be sure to place the JTextArea within a JScrollPane to support scrolling.

  2. Set the JToolBar to not be floatable so that it cannot be dragged outside the frame.

    If you prefer to have the toolbar floatable, you can skip this step.

  3. Create a JMenuBar and add it to the JFrame.

  4. Setup the first menu on the JMenuBar. Create a JMenu labeled "File" with JMenuItems under it of "New", "Open", "Save", and "Close". Place a separator between the "Save" and "Close" entries.

  5. Add a keyboard mnemonic to each item. Use the first letter of the label.

  6. For each JMenuItem, create an anonymous ActionListener to process the event and call the appropriate method to handle the event.
    • New - doNewCommand()
    • Open - doOpenCommand()
    • Save - doSaveCommand()
    • Close - doCloseCommand(statusCode)

  7. Add the Help JMenu, with the About JMenuItem. Add a key accelerator for each of 'H' and 'A' respectively. And have the action handler of the About menu item call doAboutCommand().

  8. In the doAboutCommand() method, create a modal JDialog named dialog to display the 'About' information. Have the dialog title be "About...". You will need to declare the variable final so it will be usable in the internal event handler.

  9. Once the menus are setup, its time to setup the toolbar. Create four JButtons to place on the toolbar, labeled New, Open, Save, and About. Add a separator between Save and About.

  10. For each button on the toolbar, add a tooltip, with an appropriate message.

  11. For each button, create an anonymous ActionListener to process the event and call the appropriate method to handle the event.

  12. Save everything and compile the program. Then run it to see the results.

    As a precautionary behavior, the Save command appends a "1" to the end of the filename you desire to save the file as. This attempts to prevent you from accidently overwriting your source code.

result.gif

Where help exists, the task numbers above are linked to the step-by-step help page.

The following JavaTM source file represents a solution to this Magercise.

Solution

Magercise 7

Short Course

About This Short Course

Copyright © 1998-1999 MageLang Institute. All Rights Reserved.


[ This page was updated: 19-Nov-99 ]

Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-99 Sun Microsystems, Inc.
All Rights Reserved. Legal Terms. Privacy Policy.