JOptionPane Example
JOptionPane has four types of dialog: Message dialog, Confirmation dialog, Input dialog, and Options dialog. The Message dialog type is called from the method JOptionPane.showMessageDialog() and displays a message to the user with appropriate controls according to the message type value of the fourth parameter. This program will show each of the different message types with the JOptionPane.ERROR_MESSAGE appearing last and shown to the left.
The message types are JOptionPane.PLAIN_MESSAGE, JOptionPane.QUESTION_MESSAGE, JOptionPane.WARNING_MESSAGE, JOptionPane.INFORMATION_MESSAGE, and JOptionPane.ERROR_MESSAGE.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// JOptionPane Example
import javax.swing.*;
class JOptionPaneExample {
public static void main(String[] args) {
String s = "This is the message";
String t = "This is the title";
JOptionPane.showMessageDialog(null, s);
JOptionPane.showMessageDialog(null, s, t, JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog(null, s, t, JOptionPane.QUESTION_MESSAGE);
JOptionPane.showMessageDialog(null, s, t, JOptionPane.WARNING_MESSAGE);
JOptionPane.showMessageDialog(null, s, t, JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, s, t, JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
12-17 Each line causes a message dialog to be output.
Note that when the parentComponent of the method (the first parameter) is not null the Dialog is shown centrally in the parentComponent.
