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
 

Help is available for each task, or you can go straight to the solution source code.

Task 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.

Add to the constructor:

Container content = getContentPane();
JToolBar toolbar = new JToolBar();
content.add (toolbar, BorderLayout.NORTH);
pane = new JTextArea ();
JScrollPane sPane = new JScrollPane (pane);
content.add (sPane, BorderLayout.CENTER);
statusInfo = new JLabel();
content.add (statusInfo, BorderLayout.SOUTH);

Task 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.

toolbar.setFloatable (false);

Task 3

Create a JMenuBar and add it to the JFrame.
JMenuBar menuBar = new JMenuBar();
setJMenuBar (menuBar);

Task 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.
JMenu file = new JMenu ("File");
JMenuItem item;
file.add (item = new JMenuItem ("New"));
file.add (item = new JMenuItem ("Open"));
file.add (item = new JMenuItem ("Save"));
file.addSeparator();
file.add (item = new JMenuItem ("Close"));
menuBar.add (file);

Task 5

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

At the appropriate location in the source, add the following lines:

file.setMnemonic (KeyEvent.VK_F);
item.setMnemonic (KeyEvent.VK_N);
item.setMnemonic (KeyEvent.VK_O);
item.setMnemonic (KeyEvent.VK_S);
item.setMnemonic (KeyEvent.VK_C);

Task 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)

At the appropriate location in the source, add the following lines:

item.addActionListener (new ActionListener() {
  public void actionPerformed (ActionEvent e) {
    doNewCommand();
  }
});
...
item.addActionListener (new ActionListener() {
  public void actionPerformed (ActionEvent e) {
    doOpenCommand();
  }
});
...
item.addActionListener (new ActionListener() {
  public void actionPerformed (ActionEvent e) {
    doSaveCommand();
  }
});
...
item.addActionListener (new ActionListener() {
  public void actionPerformed (ActionEvent e) {
    doCloseCommand (0);
  }
});

Task 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().
JMenu help = new JMenu ("Help");
help.setMnemonic (KeyEvent.VK_H);
help.add (item = new JMenuItem ("About"));
item.setMnemonic (KeyEvent.VK_A);
item.addActionListener (new ActionListener() {
  public void actionPerformed (ActionEvent e) {
    doAboutCommand();
  }
});
menuBar.add (help);

Task 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.
final JDialog dialog = 
  new JDialog (this, "About...", true);

Task 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.
JButton jb;
toolbar.add (jb = new JButton ("New"));
toolbar.add (jb = new JButton ("Open"));
toolbar.add (jb = new JButton ("Save"));
toolbar.addSeparator();
toolbar.add (jb = new JButton ("About"));

Task 10

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

At the appropriate location in the source, add the following lines:

jb.setToolTipText ("New File");
jb.setToolTipText ("Open File");
jb.setToolTipText ("Save File");
jb.setToolTipText ("Help - About");

Task 11

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

At the appropriate location in the source, add the following lines:

jb.addActionListener (new ActionListener() {
  public void actionPerformed (ActionEvent e) {
    doNewCommand();
  }
});
...
jb.addActionListener (new ActionListener() {
  public void actionPerformed (ActionEvent e) {
    doOpenCommand();
  }
});
...
jb.addActionListener (new ActionListener() {
  public void actionPerformed (ActionEvent e) {
    doSaveCommand();
  }
});
...
jb.addActionListener (new ActionListener() {
  public void actionPerformed (ActionEvent e) {
    doAboutCommand();
  }
});

Task 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.

javac TextEdit.java
java TextEdit

If you are using Swing with JDK 1.1.+, make sure your CLASSPATH environment variable has the swingall.jar file in it.

Return to Magercise 6

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.