JaVa
   

The JOptionPane

Option panes are basically small dialog windows that are easy and quick to implement. They have many uses, ranging from simply displaying a message to the user to taking a complete set of data from the user. Let's look at an example app that demonstrates how to implement option panes into your apps.

Code Listing 19: Using the JOptionPane
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class OptionPanesExample extends JFrame implements
 ActionListener
{
 public static void main(String[] argv)
 {
 OptionPanesExample mainApp = new OptionPanesExample();
 }
 public OptionPanesExample()
 {
 super("Option Panes Example");
 setBounds(0, 0, 350, 200);
 getContentPane().setLayout(null);
 setDefaultCloseOperation(EXIT_ON_CLOSE);
 // Create the buttons...
 messageDialogButton = new JButton("Message Dialog");
 messageDialogButton.setBounds(10, 20, 150, 25);
 warningDialogButton = new JButton("Warning Dialog");
 warningDialogButton.setBounds(10, 50, 150, 25);
 errorDialogButton = new JButton("Error Dialog");
 errorDialogButton.setBounds(10, 80, 150, 25);
 inputDialogButton = new JButton("Input Dialog");
 inputDialogButton.setBounds(175, 20, 150, 25);
 confirmationDialogButton = new JButton("Confirmation
 Dialog");
 confirmationDialogButton.setBounds(175, 50, 150, 25); componentDialogButton = new JButton("Component Dialog");
 componentDialogButton.setBounds(175, 80, 150, 25);
 // Add the action listeners
 messageDialogButton.addActionListener(this);
 warningDialogButton.addActionListener(this);
 errorDialogButton.addActionListener(this);
 inputDialogButton.addActionListener(this);
 confirmationDialogButton.addActionListener(this);
 componentDialogButton.addActionListener(this);
 // Add the objects to the content pane...
 getContentPane().add(messageDialogButton);
 getContentPane().add(warningDialogButton);
 getContentPane().add(errorDialogButton);
 getContentPane().add(inputDialogButton);
 getContentPane().add(confirmationDialogButton);
 getContentPane().add(componentDialogButton);
 setVisible(true);
 }
 public void actionPerformed(ActionEvent e)
 {
 if(e.getSource() == messageDialogButton)
 {
 JOptionPane.showMessageDialog(this, "This is a message!",
 "A Message", JOptionPane.INFORMATION_MESSAGE);
 }
 else if(e.getSource() == warningDialogButton)
 {
 JOptionPane.showMessageDialog(this, "Warning! Warning!
 This is a warning message!", "Warning",
 JOptionPane.WARNING_MESSAGE);
 }
 else if(e.getSource() == errorDialogButton)
 {
 JOptionPane.showMessageDialog(this, "A very serious error
 has occured! Well not really but you get the idea!",
 "Nasty Evil Error", JOptionPane.ERROR_MESSAGE);
 }
 else if(e.getSource() == inputDialogButton)
 {
 String feedback = JOptionPane.showInputDialog(this,
 "Enter some text");
 JOptionPane.showMessageDialog(this, feedback, "The text
 you entered...", JOptionPane.INFORMATION_MESSAGE);
 }
 else if(e.getSource() == confirmationDialogButton)
 {
 int result = JOptionPane.showConfirmDialog(this, "Are you sure?");
 if(result == JOptionPane.YES_OPTION)
 {
 JOptionPane.showMessageDialog(this, "You were sure!",
 "Result", JOptionPane.INFORMATION_MESSAGE);
 }
 else if(result == JOptionPane.NO_OPTION)
 {
 JOptionPane.showMessageDialog(this, "You were not
 sure!", "Result", JOptionPane.INFORMATION_MESSAGE);
 }
 else if(result == JOptionPane.CANCEL_OPTION)
 {
 JOptionPane.showMessageDialog(this, "You pressed
 cancel!", "Result", JOptionPane.INFORMATION_MESSAGE);
 } }
 else if(e.getSource() == componentDialogButton)
 {
 // The component dialog is a little more complex...
 Object[] theMessages = new Object[2];
 String label = "Please tick the check box";
 // Assign the first message (a simple label)
 theMessages[0] = label;
 JCheckBox checkbox = new JCheckBox("The Checkbox");
 theMessages[1] = checkbox;
 String[] options = {"Yes", "No", "Not sure", "Huh?"};
 int result = JOptionPane.showOptionDialog(this,
 theMessages,
 "The Title",
 JOptionPane.DEFAULT_OPTION,
 JOptionPane.INFORMATION_MESSAGE,
 null,
 options,
 options[0]);
 JOptionPane.showMessageDialog(this,
 String.valueOf(result), "And the result was...",
 JOptionPane.INFORMATION_MESSAGE);
 }
 }
 JButton messageDialogButton;
 JButton warningDialogButton;
 JButton errorDialogButton;
 JButton inputDialogButton;
 JButton confirmationDialogButton;
 JButton componentDialogButton;
}



Java End example

When we execute the app, we will see a window with six buttons available in it. Each button allows us to sample each of the different types of option panes available to us. This can be seen in the following screen shot:

Java ScreenShot
Screenshot: The Option Panes Example app

Let's now look at each of the different option panes (dialogs) individually.

Heavy going, eh? Notice that the showOptionDialog method returns an integer, which we store in a variable called result. This will contain the index of the button array options, which represents the button the user has clicked on (i.e., if the user clicks on Yes, the result will be 0; if the user clicks on No, the result will be 1, etc.).

JaVa
   
Comments