Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Listing 18.2 Exit Dialog Box Using ActionOptionPane

package com.foley.test;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;

import com.foley.dialogs.*;
import com.foley.utility.*;

/**
* An application that tests the ActionOptionPane dialog.
*
* @author Mike Foley
**/public class ActionOptionTest extends JFrame {

       public static ActionOptionTest frame;

      /**
       * ActionOptionTest, null constructor.
       **/
      public ActionOptionTest() {
          this( null );
      } // ActionOptionTest
      /**    
       * ActionOptionTest, constructor.
       *
       * @param title The title for the frame.
       **/
       public ActionOptionTest( String title ) {
           super( title ); 
       } // ActionOptionTest 

      /**
       * frameInit, from JFrame
       *
       * Create the contrent for this frame.
       **/
       protected void frameInit() {

           //
           // Let our super create the content and associated panes.
           //
           super.frameInit();

          JMenuBar menubar = createMenu();
          setJMenuBar( menubar );

      } // frameInit

      /**
       * Create the menu for the frame.
       *
       * @return The menu for the frame.
       **/
       protected JMenuBar createMenu() {

           //
           // Load the images used.
           //
           Icon exitIcon = new ImageIcon( “exit.gif” );

           JMenuBar menubar = new JMenuBar();

           JMenu file = new JMenu( “File” );

           JMenuItem item = new JMenuItem(“Exit”,exitIcon );
           item.setHorizontalTextPosition( SwingConstants.RIGHT );
           item.setMnemonic( KeyEvent.VK_X );
           item.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_X,
             Event.CTRL_MASK ) );
           item.addActionListener( new ActionListener() {
               public void actionPerformed( ActionEvent e ) {
                    showExitConfirmation( ( Component)e.getSource() );
               }
           } );
           file.add( item );

           menubar.add( file );

           return( menubar );

} // createMenu

/**
 * Display the Action aware exit confirmation dialog.
 *
 * @param parent The parent for the dialog. May be null.
**/
private void showExitConfirmation( Component parent ) {

   Action[] actions = new Action[2];
   actions[0] = new ExitAction( “OK” );
   actions[1] = new NoOpAction(“Cancel” );
   JOptionPane pane = new ActionOptionPane( “Exit Application”,
JOptionPane.QUESTION_MESSAGE,
JOptionPane.DEFAULT_OPTION,
                                  null, actions,
                                  actions[1] );

JDialog dialog = pane.createDialog( parent, “Exit Confirmation” );
        dialog.setVisible( true );

        //
// The action has been executed.
        // If the button which was pressed is needed,
        // it can be queried with the getValue method.
        //
        Object selected = pane.getValue();

    }

    /**
     * Application entry point.
     * Create the frame, and display it.
     *
     * @param args Command line parameter. Not used.
     **/
     public static void main( String args[] ) {

        JFrame frame = new ActionOptionTest( “ActionOptionPane Test” );
        frame.pack();
        frame.setVisible( true );

    } // main

} // ActionOptionTest

package com.foley.utility;

import java.awt.event.*;

import javax.swing.*;

/**
 * Class NoOpAction
 * <p>
 * An action which does nothing.  This is sometimes
 * needed for Action aware components.
 * <p>
 * @author Mike Foley
 * @version 1.2
 **/
public class NoOpAction extends AbstractAction {

    /**
    * The default name used in a menu.
    **/
   private static final String DEFAULT_NAME = “NoOp”;

   /**
    * NoOpAction, default constructor.
    * <p>
    * Create the action with the default name.
    **/
public NoOpAction() {
           this( DEFAULT_NAME, null );
       } // NoOpAction

       /**
        * NoOpAction, constructor.
        * <p>
        * Create the action with the given name.
        * <p>
        * @param name The name for the Action.
        **/
    public NoOpAction( String name ) {
           this( name, null );
       } // NoOpAction

       /**
        * putValue.
        *
        * The AbstractAction class putValue method may not
        * be passed a null newValue parameter. Filter those
        * call here.
        * @TODO Remove calls to putValue with a null newValue. 
        *
        * @param key The key for the value.
        * @param newValue The value for the associated key.
        **/
       public void putValue(String key, Object newValue) {
           if( key != null && newValue != null )
               super.putValue( key, newValue );
      }

       /**
        * actionPerformed, from ActionListener
        * <p>
        * Do nothing.
        * <p>
        * @param event The event causing the action to fire.
        **/
        public void actionPerformed( ActionEvent event ) {
       } // actionPerformed

} // NoOpAction


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.