Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


You can also add an instance of a MenuItem class to a menu:

// Make a “Save” menu item
MenuItem saveMenuItem = new MenuItem( “Save” );

// Add the “Save” option to the file menu
fileMenu.add( saveMenuItem );

You can enable and disable menu items by using setEnabled method. When you disable a menu item, it still appears on the menu, but it usually appears in gray (depending on the windowing system). You cannot select disabled menu items. The format for setEnabled is this:

// Disable the save option from the file menu 
saveMenuItem.setEnabled( false );

// Enable the save option again
saveMenuItem.setEnabled( true );

In addition to menu items, you can add submenus and menu separators to a menu. A separator is a line that appears on the menu to separate sections of the menu. To add a separator, just call the addSeparator method:

fileMenu.addSeparator();

To add a submenu, make a new instance of a menu and add it to the current menu:

Menu printSubmenu = new Menu( “Print” );
fileMenu.add( printSubmenu );
printSubmenu.add( “Print Preview” );
         // Add print preview as option on Print menu
printSubmenu.add( “Print Document” );
         // Add print document as option on Print menu

You can also add special check box menu items. These items function like check box buttons. The first time you select one, it becomes checked or “on.” The next time you select it, it becomes unchecked or “off.” The code to add a check box menu item:

CheckboxMenuItem autoSaveOption = new CheckboxMenuItem( “Auto-save” );
fileMenu.add( autoSaveOption );

You can check to see whether a check box menu item is checked with getState:

if ( autoSaveOption.getState() )
{
     // autoSaveOption is checked, or “on”
}
else
{
     // autoSaveOption is off
}

You can set the current state of a check box menu item with setState:

autoSaveOption.setState( true );

Typically, menus are added to a menu bar in a left-to-right fashion. Some windowing systems, such as Microsoft Windows 95, have a special “Help” menu that is on the far right of a menu bar. You can add such a menu to your menu bar with the setHelpMenu method:

Menu helpMenu = new Menu();
myMenuBar.setHelpMenu( helpMenu );

Using AWT Menus Whenever a menu item is selected, it generates an ActionEvent. Just add an ActionListener for each menu item to handle events.

Listing 38.4 shows an application that sets up a simple File menu with New, Open, and Save menu items, a check box called Auto-Save, and a Print submenu with two menu items on it:

Listing 38.4 MenuApplicationAdd Menus and Menu Items to a Menu in a Frame


import java.awt.*;
import java.applet.*;

public class MenuApplication extends Frame {
  public static void main( String[] args ) {
    new MenuApplication();
  }
    
  public MenuApplication() {
    //  Construct the Frame
    super( “Menu Example” );
        
    // Add the menu bar
    MenuBar theMenuBar = new MenuBar();
    setMenuBar( theMenuBar );
        
    // Make the file menu and add it to the menubar...
    Menu aFileMenu = new Menu( “File” );
    theMenuBar.add( aFileMenu );
        
    // Add the New and Open menuitems
    aFileMenu.add( new MenuItem( “New” ) );
    aFileMenu.add( new MenuItem( “Open” ) );
        
    // Add a (disabled) Save menuitem
     MenuItem theSaveMenuItem = new MenuItem( “Save” );
     theSaveMenuItem.disable();
     aFileMenu.add( theSaveMenuItem );

    // Add an Auto-Save checkbox, followed by a separator
    aFileMenu.add( new CheckboxMenuItem( “Auto-Save” ) );
    aFileMenu.addSeparator();

    // Add the Print submenu
    Menu thePrintSubmenu = new Menu( “Print” );
    aFileMenu.add( thePrintSubmenu );
    thePrintSubmenu.add( “Print Preview” );
    thePrintSubmenu.add( “Print Document” );

    // Resize the frame before it can be shown
    setSize( 300, 200 );
        
    // Make the frame appear on the screen
    show();
  }
}

Figure 38.13 shows the output from the MenuApplication program, with the Print Document option in the process of being selected.


FIGURE 38.13  The AWT provides a number of popular menu features, including checked menu items, disabled menu items, and separators.

AWT Dialogs

Like Frames, Dialogs are windows. Unlike Frames, Dialogs are designed to be pop-up windows that are not quite as flexible as frames. Dialogs are used for things such as “Are you sure you want to quit?” pop-ups, better known as message boxes. You can set a dialog to be either “modal” or “non-modal.” The term modal means the dialog box blocks input to other windows while it is being shown. This technique is useful for dialogs where you want to stop everything and get a crucial question answered, such as “Are you sure you want to quit?” An example of a non-modal dialog box is a control panel that changes settings in an application while the application continues to run.

Making Dialogs To make a dialog, you must first have a frame. A dialog cannot belong directly to an applet. However, an applet may instantiate a frame to which the dialog can then belong. You must specify whether a dialog is modal or non-modal when you instantiate it; you cannot change its modality after it has been built. The following example makes a modal dialog whose parent is myFrame:

// true means model dialog 
Dialog myDialog = new Dialog( myFrame, true );

You can also give a dialog a title:

Dialog myDialog = new Dialog( myFrame, “A Non-Modal Dialog”, false );    


NOTE:  Because a dialog cannot belong directly to an applet, your use of dialogs can be somewhat limited. One solution is to make a dummy frame as the dialog’s parent. Unfortunately, you cannot make modal dialogs this way because only the frame and its children would have their input blocked—the applet would continue on its merry way. A better solution is to use the technique shown in HelloPlus.java (Listing 38.3) in which you make a standalone application using frames and then have a “bootstrap” applet make a frame and run the real applet in it.

After you have instantiated a dialog, you can make it visible using the show method:

myDialog.show();

Dialog Features The Dialog class has several methods in common with the Frame class:

     void setResizable( boolean );
     boolean isResizable();
     void setTitle(String);
     String getTitle();

In addition, the isModal method returns true if the dialog is modal.

A Reusable OK Dialog Box Listing 38.5 shows the OKDialog class that provides an OK dialog box that displays a message and waits for you to click OK.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.