The JOptionPane in Java Swing

Home  »  Java  »  Advance Java  »  Java Swing II  »  The JOptionPane Class


JOptionPane is a convenience class containing the functionality to crate option panes that are modal. JOptionPane is a direct subclass of JComponent and is stored in the package.javax.swing.

The JOptionPane static methods are used to create and display option panes of type JDialog i.e. dialog boxes that ask a question, warn a user, or provide a brief, important message. When a system crashes, a dialog box appear and breaks the bad news, on deleting files a dialog box appears to make sure whether the user really wants to do that.

These windows are an effective way to communicate a user without the overhead of creating a new class to represent the window, adding components to it, writing event handling methods to take input. All of these things are handled automatically when one of the standard modal dialog boxes offered by JOptionPane class is used.

There are four standard modal dialog boxes

  1. ConfirmDialog Asks a question, with buttons for Yes, NO, and Cancel responses.
  2. InputDialog Prompts user to text input.
  3. MessageDialog Displays a message.
  4. OptionDialog Comprises all three of the other dialog box types.

Each of these boxes has its own constructor in the JOptionPane class.

Confirm Dialog Boxes

The eaisest way to create a Yes/No/Cancel dialog box is with the showConfirmDialog(Component, Object) method call.

The Component argument specifies the container that should be considered the parent of the dialog box, and this information is used to determine where on the screen the dialog window should be displayed. If null is used instead of a container, or if the container is not a Frame object, the dialog box will be centered on the screen.

The second argument, Object, can be a string, a component, or a Icon Object. If it's a string, that text will be displayed in the dialog box. If it's a component or an icon, that object will be displayed in place of a text messsage.

This method returns one of three possible integer values, each a class variable of JOptionPane: YES_OPTION, NO_OPTION, and CANCEL_OPTION.

Example:

The following example uses a confirm dialog box with a text message and stores user response in the variable response.



More options for the confirm dialog:


showConfirmDialog(Compoent, Object, String, int, int)

The first two arguments are the same as those in other showConfirmDialog() methods. The last three arguments are the following:

  1. A String that will be displayed in the dialog box's title bar.

  2. An integer that indicates which option buttons will be shown. It should be equal to the class variables YES_NO_CANCEL_OPTION displays YES_NO and CANCEL buttons whereas, YES_NO_OPTION displays YES and NO buttons.

  3. An integer that describes the kind of dialog box it is, using the class variables ERROR_MESSAGE, INFORMATION_MESSAGE, PLAIN_MESSAGE, QUESTION_MESSAGE, or WARNING_MESSAGE. This argument is used to determine which icon is displayed in the dialog box along with the message.


Example:




Message Dialog Boxes

A message dialog box is a simple window that displays information.

This can be created with a call to the showMessageDialog(Component, Object) method. As with other dialog boxes, the arguments are the parent component and the string component, or icon to display.

Unlike the other dialog boxes, message dialog boxes do not return any kind of response value.

Example:

The following statement creates the message dialog.



Also the message input dialog box can be created with the showMessageDialog(Component, Object, String, int) method. The use is identical to the showInputDialog() method, with the same arguments, except that showMessageDialog() does not return a value.

Example:

The following statement creates the message dialog using this method.





Example:
The following example displays a Login screen as shown in below diagram. A check is done to find whether the login name is "Laura" and password is "Cath". Incase of right login name and password a message dialog is displayed informing the user about the correct login name and password. Incase of wrong login name and password, a confirm dialog is displayed with "Yes" and "No" options.



C:\>jdk1.4\bin>javac confirmdialog.java
C:\>jdk1.4\bin>java confirmdialog



Output of confirmdialog.class


Message displayed when a wrong data is entered.


Message displayed when correct data is entered.


Message displayed when the 'Cancel' button is clicked.


Download Complete Program

  -  



Java Swing-II


JOptionPane Class Input Dialog Boxes JSliders
JProgressBars JTables JTabbedPane
JMenu JPopupMenu JToolBar
JFileChooser JScrollBar JScrollPane
JRootPane JTextPane JTree
JSplitPane JDesktopPane JEditorPane




1 comment: