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
-
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.
-
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.
-
Create a
JMenuBar and add it to the JFrame .
-
Setup the first menu on the
JMenuBar . Create a
JMenu labeled "File" with JMenuItem s
under it of "New", "Open", "Save",
and "Close". Place a separator between the "Save"
and "Close" entries.
-
Add a keyboard mnemonic to each item. Use the first letter of the label.
-
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)
-
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() .
-
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.
-
Once the menus are setup, its time to setup the toolbar. Create four
JButton s to place on the toolbar, labeled New, Open, Save, and
About. Add a separator between Save and About.
-
For each button on the toolbar, add a tooltip, with an appropriate message.
-
For each button, create an anonymous
ActionListener to process
the event and call the appropriate method to handle the event.
-
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.
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.
Copyright © 1998-1999
MageLang Institute.
All Rights Reserved.
|