In addition to console input and output (to be discussed later in class), Java
has a simple mechanism to ask a user questions and to receive answers.
The Java class
JOptionPane
is commonly used for this purpose. The class JOptionPane resides in package
javax.swing.
It defines the following useful static methods
Example:
import javax.swing.*;
class Test1 {
public static void main(String args[]) {
JOptionPane.showMessageDialog(null, "hello world" );
System.exit(0);
}
}
when run, the program produces this message box:
Explanation of parameters:
Note !
The Java line "System.exit(0);" is added to ensure that the program ends once the dialog is completed. If you want your program
to continue after it returns from the dialog, then don't use this line. Use the line as the final line in your main program.
Example:
Explanation of parameters:
Example:
Explanation of parameters:
Example:
Explanation of parameters:
JOptionPane.showConfirmDialog
This static method is meant to get answers from the user. In its simplest form the user
can select from a "yes", "no" or "cancel" button.
import javax.swing.*;
class Test2 {
public static void main(String args[]) {
int i = JOptionPane.showConfirmDialog(null, "hello world" );
System.out.println("result: " + i);
System.exit(0);
}
}
when run, the program produces this message box:
Notice that the showConfirmDialog method has a return value of type int. It indicates
which button the user has pressed. The "yes" button return 0, the "no" button returns 1,
the "cancel" button returns 2.
There are 3 more showConfirmDialog methods in the JOptionPane class. They have additional
parameters to change the question-mark symbol into others, to change the title of the dialog box, and
to change the button choices that are presented to the user.
JOptionPane.showInputDialog
This static method is meant to ask the user a question, and get the user's answer.
import javax.swing.*;
class Test3 {
public static void main(String args[]) {
String s = JOptionPane.showInputDialog("hello world" );
System.out.println("result: " + s);
System.exit(0);
}
}
when run, the program produces this message box:
Notice that the showInputDialog method has a return value of type String. It contains
whatever the user has typed in.
There are several more showConfirmDialog methods in the JOptionPane class. They have additional
parameters to pre-fill the user's answer field with default text, to change the title of the dialog box, and
to change the he question-mark symbol into others.
JOptionPane.showOptionDialog
This static method is meant to be a combination of the other 3 methods. It has a parameter that specifies
whether the dialog is meant to be a "message", "confirm" or "input" dialog. All other parameters of the
other methods can be specified.
It can also be used for quite flexible input questions, where the user can select
from a set of choices.
import javax.swing.*;
class Test4 {
public static void main(String args[]) {
Object []buttons = {"add","delete","move"};
int i = JOptionPane.showOptionDialog(null,
"Where do you want to go ?", "dialog title",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
buttons, buttons[0]);
System.out.println("result: " + i);
System.exit(0);
}
}
when run, the program produces this message box:
Notice that the showOptionDialog method has a return value of type int. It returns
index from the buttons array that the user has clicked.
More Information
Additional examples can be found in the
"How to Make Dialogs"
chapter of the online Java Tutorial.
The source code of the 4 example Java programs is here.