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