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


Listing 38.5 OKDialogPop Up a Simple Dialog Box with an OK Button


import java.awt.*;

public class OKDialog extends Dialog {
  protected Button  fOKButton;
  protected static Frame  fFrame;
    
  public OKDialog( Frame theParent, String theMessage )
  {
    super( theParent, true );    // Call the parent’s constructor
        
    // This Dialog box uses the GridBagLayout.
    GridBagLayout theGridBagLayout = new GridBagLayout();
    GridBagConstraints theConstraints = new GridBagConstraints();
        
    // Make the OK button and the message to display
    fOKButton = new Button( “OK” );
    Label aMessageLabel = new Label( theMessage );
        
    setLayout( theGridBagLayout );
        
    // The message should not fill, it should be centered within this area, with
    // some extra padding.  The gridwidth of REMAINDER means this is the only
    // thing on its row, and the gridheight of RELATIVE means there should only
    // be one thing below it.
    theConstraints.fill = GridBagConstraints.NONE;
    theConstraints.anchor = GridBagConstraints.CENTER;
    theConstraints.ipadx = 20;
    theConstraints.ipady = 20;
    theConstraints.weightx = 1.0;
    theConstraints.weighty = 1.0;
    theConstraints.gridwidth = GridBagConstraints.REMAINDER;
    theConstraints.gridheight = GridBagConstraints.RELATIVE;
    theGridBagLayout.setConstraints( aMessageLabel, theConstraints );
    add( aMessageLabel );

    // The button has no padding, no weight, takes up minimal width, and
    // is the last thing in its column.
    theConstraints.ipadx = 0;
    theConstraints.ipady = 0;
    theConstraints.weightx = 0.0;
    theConstraints.weighty = 0.0;
    theConstraints.gridwidth = 1;
    theConstraints.gridheight = GridBagConstraints.REMAINDER;
    theGridBagLayout.setConstraints( fOKButton, theConstraints );
    add( fOKButton );

    // Pack is a special window method that makes the window take up the minimum
    // space necessary to contain its components.
    pack();
  }

  // The action method just waits for the OK button to be clicked;
  // when it is it hides the dialog, causing the show() method to return
  // back to whoever activated this dialog.

  public boolean action( Event theEvent, Object whichAction ) {
    if ( theEvent.target == fOKButton ) {
      setVisible( false );
      if ( fFrame != null )
        fFrame.setVisible( false );
    }
    return( true );
  }

  // Shortcut to make a frame automatically
  public static void makeOKDialog( String theDialogString ) {
    if ( fFrame == null )
      fFrame = new Frame( “Dialog” );
    OKDialog theOKDialog = new OKDialog( fFrame, theDialogString );
        
    // Shrink the frame to just fit the dialog
    fFrame.setSize( theOKDialog.getSize().width, theOKDialog.getSize().height );

    theOKDialog.show();
  }
}

The DialogApplet in Listing 38.6 pops up an OKDialog whenever a button is pressed.

Listing 38.6 DialogApplet.javaTest Your New OKDialog with This Applet


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

public class DialogApplet extends Applet {
  protected Button fLaunchButton;
    
  public void init() {
    fLaunchButton = new Button( “Show OK Dialog” );
    add( fLaunchButton );
    fLaunchButton.addActionListener( new ActionListener() { 
      public void actionPerformed( ActionEvent e ) { 
        doLaunchClicked(); } } );
  }

  public void doLaunchClicked() {
    OKDialog.makeOKDialog( “Press OK to dismiss this dialog” );
  }
}

Figure 38.14 shows the DialogApplet with the OK dialog popped up.


FIGURE 38.14  The OKDialog class makes a pop-up dialog box with an OK button.

The Swing Architecture

When Java was first introduced, the only graphical user interface (GUI) available was the Abstract Windowing Toolkit (AWT). Recall from the first section of this chapter, “Interacting with People by Using Java,” that AWT components are considered to be “heavyweight” in that each Java component has a peer object from the native GUI.

Sun also offers “lightweight” components that don’t have a peer object. Instead, they offer “pluggable look and feel.” If the end user is running on a Windows 95 machine but prefers the look and feel of Sun’s Motif interface, for example, the user can select that interface in the running application. When Sun developed these lightweight components, it did so as part of a project called “Swing,” so the lightweight user interface components are usually called Swing components.

Swing components are one part of the Java Foundation Classes (JFC). Figure 38.15 illustrates the relationship of the AWT and the Swing components to the JFC.


NOTE:  Many of the Swing components are derived from their AWT counterparts. Most developers, however, reserve the term “AWT component” for the heavyweight components, and refer to the components in javax.swing simply as “Swing components.”


FIGURE 38.15  The Swing components are the lightweight user-interface components of the JFC.

Understanding the JFC

Although the JFC was released well before JDK 1.2, Sun didn’t make a big deal about it. Most of Sun’s description of the new JFC has centered on Swing, so many people have the idea that Swing is all there is to the JFC and that the JFC came out as part of JDK 1.2. The link is so strong that Sun unofficially calls JFC 1.1 “Swing 1.0.”

In fact, the JFC includes

  The lightweight user interface components (code named Swing)
  The delegation event model
  Printing capability
  Clipboard data transfer
  Better integration with system colors
  Mouseless operation
  Drag-and-drop operation
  Better support for assistive technology for people with disabilities
  Improved 2D graphics operations


NOTE:  Before JFC was developed, Netscape developed a set of classes called the Internet Foundation Classes, or IFC. Microsoft has also developed the Application Foundation Classes, or AFC. Netscape has announced that the JFC supersedes the IFC, leaving Microsoft and Sun to battle it out.

A Short Tour of Swing

Sun supplies Swing in 14 packages:

  javax.swing—Components, adapters, default component models and interfaces
  javax.swing.basic—The user interface classes (known as delegates) for the Windows look and feel
  javax.swing.beaninfo—Support classes for use when Swing components are used as JavaBeans
  javax.swing.border—The Border interface and classes, which define specific border-rendering styles
  javax.swing.event—Swing-specific event types and listeners
  javax.swing.jlf—The user interface classes for the Java look and feel (sometimes known as “Metal”)


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.