25-07-2012, 02:03 PM
JOptionPane class
JOptionPaneclass.ppt (Size: 225.5 KB / Downloads: 177)
Dialog Boxes
A dialog box is a small graphical window that displays a message to the user or requests input.
A variety of dialog boxes can be displayed using the JOptionPane class.
Two of the dialog boxes are:
Message Dialog - a dialog box that displays a message.
Input Dialog - a dialog box that prompts the user for input.
Using the import Statement
The JOptionPane class is not automatically available to your Java programs.
The following statement must be before the program’s class header:
import javax.swing.JOptionPane;
This statement tells the compiler where to find the JOptionPane class.
Message Dialogs
JOptionPane.showMessageDialog method is used to display a message dialog.
JOptionPane.showMessageDialog(null, "Hello World");
The second argument is the message that is to be displayed
Input Dialogs
An input dialog is a quick and simple way to ask the user to enter data.
The dialog displays a text field, an Ok button and a Cancel button.
If Ok is pressed, the dialog returns the user’s input.
If Cancel is pressed, the dialog returns null.
The System.exit() Method
A program that uses JOptionPane does not automatically stop executing when the end of the main method is reached.
Java generates a thread, which is a process running in the computer, when a JOptionPane is created.
If the System.exit method is not called, this thread continues to execute.
A program that uses JOptionPane does not automatically stop executing when the end of the main method is reached.
Java generates a thread, which is a process running in the computer, when a JOptionPane is created.
If the System.exit method is not called, this thread continues to execute.