Home : Course Map : Chapter 7 : Java : Supplements :
Dialogs with JOptionPane
JavaTech
Course Map
Chapter 7

Introduction
Event Overview
Event Processing
Button Events
  Demo 1
 Demo 2
Mouse Events
  Demo3

More Components
  Demo 4  Demo 5
  Demo 6  Demo 7

LayoutManagers-1
  Demo 8     Demo 9
  Demo 10  Demo 11
  Demo 12

LayoutManagers-2
  Demo 13  Demo 14
  Demo 15  Demo 16
  Demo 17

Inner Classes
Anonymous Class
Adapter Classes
  Demo 18  Demo 19
Frames & Menus
  Demo 20  Demo 21
Exercises

    Supplements
AWT Components
  Button
     Demo 1
  Canvas
     Demo 2
  AWT GUI Demo
     Demo 3
Swing Dialogs
JOptionPane Dialog
  Demo 1
JDialog
  Demo 2
UI Enhancement: P1
  Demo 1   Demo 2
  Demo 3

UI Enhancement: P2
  Demo 1
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

The methods in JOptionPane provide for a wide variety of basic dialogs. These can be warning messages, confirmations, one line text inputs, and so forth. They provide default icons to accompany the message or you supply your own.

See the How to Make Dialogs - Sun Java Tutorial and the JOptionPane specification (for Java 5.0 Specs) for details of the many options for the showMessageDialog(), showConfirmDialog(), showInputDialog(), and showOptionDialog() overloaded versions of each. (For each of these there is also a showInternalzzzzzDialog() version for internal frame interfaces.)

The applet below illustrates the essentials of these four methods. Note that in most of the overridden verisions, the first argument is a reference to a frame, which serves as the parent to which the dialog belongs with regard to its modal behavior. If this is set to null, then a default frame is used.

 
DialogsApplet.java  

 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
  * Generates various types of dialogs from options in a JComboBox.
**/
public class DialogsApplet extends JApplet
                       implements ActionListener
{
  String [] fDialogTypes =
    {"Warning", "Confirm", "Message", "Option", "Input"};
  String fDialogType = "Warning";
  String [] fOptions =
    {"Red", "Orange", "Yellow", "Green", "Blue"};

  JComboBox fChoice;
  JButton fButton;

  /** Create an interface. **/
  public void init () {
    Container content_pane = getContentPane ();

    // A panel to hold the fButton and ComboBox.
    JPanel panel_a = new JPanel (new GridLayout (1,2));

    // Create the interface to choose a type of dialog
    // and create it.
    fButton = new JButton ("Make Dialog");
    fButton.addActionListener (this);
    fButton.setBackground (Color.RED);
    panel_a.add (fButton);

    fChoice = new JComboBox (fDialogTypes);
    fChoice.addActionListener (this);
    panel_a.add (fChoice);

    // Use another panel to arrange the first
    // panel and a message at the bottom.
    JPanel panel_b = new JPanel (new BorderLayout ());

    panel_b.add ("Center", panel_a);

    panel_b.add ("South",
      new JLabel ("Generate different types of dialogs",
                  JLabel.CENTER));

    content_pane.add (panel_b);
  } // init

  /** React to actions of the fButton and combobox. **/
  public void actionPerformed (ActionEvent event) {
    // Use the type of component to find where event
    // came from.
     Object source = event.getSource ();

    if (source instanceof JButton) {
        // Issue a dialog according to the setting in the
        // combobox.
        if (fDialogType.equals ("Warning")) {
            JOptionPane.showMessageDialog (null,
              "Put a warning message here!", "A Warning Dialog",
              JOptionPane.WARNING_MESSAGE);
        } else if (fDialogType.equals ("Confirm")) {
            JOptionPane.showConfirmDialog (null,
              "Put a confirmation message here", "A Confirm Dialog",
              JOptionPane.YES_NO_CANCEL_OPTION);
        } else if (fDialogType.equals ("Message")) {
            JOptionPane.showMessageDialog (null,
              "Put a message here!", "A Message Dialog",
              JOptionPane.PLAIN_MESSAGE);
        } else if (fDialogType.equals ("Option")) {
            int index =
              JOptionPane.showOptionDialog (null,
                "Put an options message here!", "An Options Dialog",
                JOptionPane.DEFAULT_OPTION,
                JOptionPane.ERROR_MESSAGE,
                null, fOptions, fOptions[0]);

            if (index == 0)
                fButton.setBackground (Color.RED);
            else if (index == 1)
                fButton.setBackground (Color.ORANGE);
            else if (index == 2)
                fButton.setBackground (Color.YELLOW);
            else if (index == 3)
                fButton.setBackground (Color.GREEN);
            else if (index == 4)
                fButton.setBackground (Color.BLUE);

        }  else if (fDialogType.equals ("Input")) {
            JOptionPane.showInputDialog (null,
                "User input", "An Input Dialog",
                 JOptionPane.INFORMATION_MESSAGE);
        }
    } else {
            fChoice = (JComboBox)source;
            fDialogType = (String)fChoice.getSelectedItem ();
    }

  } // actionPerformed

} // class DialogsApplet

 

References & Web Resources

Latest update: Nov. 4, 2004

           Tech
Histogram UI
  Demo 1
Probablity Distrib.
  Demo 2 Demo 3
RejectionMethod
Histogram Stats
  Demo 4
Exercises

           Physics
Sim & Randomness
Custom Prob. Dist.
   Demo 1
Histogram Dist.
   Demo 2
Monte Carlo
  Demo 3
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.