
Hello everyone. This page is an English translation of a Japanese page. (The original Japanese has been slightly rewritten to make it easier to translate into English.)
We used JOptionPane of Swing this time. JOptionPane is most likely to be used for dialog display. We put a JButton in a JFrame and we tried various samples to show the JOptionPane.
The following program is used as a template.
Source
import javax.swing.JFrame;
public class Test extends JFrame {
public static void main(String[] args) {
new Test();
}
public Test() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(150, 100);
init();
setVisible(true);
}
private void init() {
// installation process
}
}
Table of Contents
- Dialog to display buttons
- JOptionPane#createDialog()
- Constructor JOptionPane(Object message)
- JOptionPane#setMessage()
- JOptionPane#setMessageType()
- JOptionPane#setOptionType()
- Pressed button
- showXxxDialogメソッド
- showConfirmDialog
- showInputDialog
- showMessageDialog
Dialog to display buttons
JOptionPane#createDialog()
JOptionPane is a class that can prompt the user for input and can also display information. Basically, it is an object that is displayed in a dialog.
The following is an example of using the constructor JOptionPane().
Source
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Test2 extends JFrame {
public static void main(String[] args) {
new Test2();
}
public Test2() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(150, 100);
init();
setVisible(true);
}
private void init() {
setLayout(new FlowLayout());
JOptionPane optionPane = new JOptionPane();
JDialog dialog = optionPane.createDialog(null, "My Test Dialog Title");
JButton button = new JButton("Open Dialog");
add(button);
button.addActionListener(event -> {
dialog.setVisible(true);
});
}
}
In the above sample, a dialog is displayed when the “Open Dialog” button is pressed.
We first used the JOptionPane#createDialog() method. This method returns a JDialog object. The first argument represents the parent frame, and the second argument represents the text to be displayed in the title bar. Next, we create a JButton object, which will display a dialog when pressed.


The string “My Test Dialog Title” will appear in the title bar. The message to be displayed, “JOptionPane message,” is initially determined by the constructor JOptionPane().
Constructor JOptionPane(Object message)
In the constructor with no arguments, the message to be displayed is fixed. To change this message, use the constructor JOptionPane(Object message). For example, it is used as follows
Source
JOptionPane optionPane = new JOptionPane("My Test Message");

Since the argument is an Object, a JPanel can be specified as the argument. For example, you can use the following.
Source
JPanel panel = new JPanel();
panel.add(new JLabel("My Panel Test Label"));
panel.add(new JTextField(20));
JOptionPane optionPane = new JOptionPane(panel);

In the above, a JPanel object is created first. A JLabel object and a JTextField object are created and added to the JPanel object.
By specifying the JPanel object as the argument of the constructor JOptionPane(), the JPanel object is displayed in the options window.
JOptionPane#setMessage()
If we do not specify the message in the constructor, we can change it later. The void setMessage(Object newMessage) method is provided for this purpose. For example, it is used as follows.
Source
JOptionPane optionPane = new JOptionPane();
optionPane.setMessage("My Test Message 2");
The message is set to “My Test Message 2”.
JOptionPane#setMessageType()
The setMessageType() method allows you to set the style of the message. The argument of this method is of type int, and the following static fields are used
Field | Description. |
---|---|
ERROR_MESSAGE | error message |
INFORMATION_MESSAGE | information message |
WARNING_MESSAGE | warning message |
QUESTION_MESSAGE | question |
PLAIN_MESSAGE | Do not use icons. |
The following is an example of use.
Source
JOptionPane optionPane = new JOptionPane();
optionPane.setMessage("My Test Message");
optionPane.setMessageType(JOptionPane.PLAIN_MESSAGE);
We specified the values in the above list as arguments to the setMessageType() method, and the result was displayed as follows. (Due to the execution environment, some labels are in Japanese.)
- ERROR_MESSAGE
-
- INFORMATION_MESSAGE
-
- WARNING_MESSAGE
-
- QUESTION_MESSAGE
-
- PLAIN_MESSAGE
-
JOptionPane#setOptionType()
In the setOptionType() method, options can be set. The button to be displayed changes depending on the value specified in the argument. The argument is of type int and specifies the following static fields.
Fields | Description. |
---|---|
DEFAULT_OPTION | “OK” |
YES_NO_OPTION | 「はい」、「いいえ」(“Yes”, “No”) |
YES_NO_CANCEL_OPTION | 「はい」、「いいえ」、「取消」(“Yes”, “No”, “Cancel”) |
OK_CANCEL_OPTION | 「OK」、「取消」 |
(Due to the execution environment, we could not check the English label.)
The following are examples of use.
Source
JOptionPane optionPane = new JOptionPane();
optionPane.setMessage("My Test Message");
optionPane.setOptionType(JOptionPane.DEFAULT_OPTION);
As a result of our specifying the values in the above list as arguments to the setOptionType() method, the following was displayed.
- DEFAULT_OPTION
-
- YES_NO_OPTION
-
- YES_NO_CANCEL_OPTION
-
- OK_CANCEL_OPTION
-
Pressed button
Which button was pressed can be obtained by the getValue() method. Depending on the button pressed, the value of the following static field is returned.
- YES_OPTION
- NO_OPTION
- CANCEL_OPTION
- OK_OPTION
- CLOSED_OPTION
The following are examples of use.
Source
private void init() {
setLayout(new FlowLayout());
JOptionPane optionPane = new JOptionPane();
optionPane.setMessage("My Test Message");
optionPane.setOptionType(JOptionPane.OK_CANCEL_OPTION);
JDialog dialog = optionPane.createDialog(null, "My Test Dialog Title");
JButton button = new JButton("Open Dialog");
add(button);
button.addActionListener(event -> {
dialog.setVisible(true);
if (optionPane.getValue().equals(JOptionPane.YES_OPTION)) {
System.out.println("Yes.");
} else if (optionPane.getValue().equals(JOptionPane.CANCEL_OPTION)) {
System.out.println("Cancel.");
}
});
}
When the “OK” button is pressed, “Yes.” is output to the console. When the “Cancel” button is pressed, “Cancel.” is output to the console.
Sponsored Links
showXxxDialogメソッド
Some static methods are provided to output the standard dialogs. There are methods of the same name with different arguments. If we ignore the distinction, the following four are listed.
- showConfirmDialog
- showInputDialog
- showMessageDialog
- showOptionDialog
In the above, the showOptionDialog method is a combination of the first three. This time, we used the first three.
showConfirmDialog
Some of the methods are overloaded. In this case, we used showConfirmDialog(Component parentComponent, Object message).
Source
private void init() {
setLayout(new FlowLayout());
JButton button = new JButton("Open Dialog");
add(button);
button.addActionListener(event -> {
int result = JOptionPane.showConfirmDialog(null, "My Test Dialog Title");
if (result == JOptionPane.YES_OPTION) {
System.out.println("Yes.");
} else if (result == JOptionPane.NO_OPTION) {
System.out.println("No.");
} else if (result == JOptionPane.CANCEL_OPTION) {
System.out.println("Cancel.");
} else if (result == JOptionPane.CLOSED_OPTION ) {
System.out.println("Closed.");
}
});
}

The returned value of JOptionPane#showConfirmDialog method is of type int. The button that was pressed can be determined as shown above.
showInputDialog
Several methods are overloaded, but this time the showInputDialog(Component parentComponent, Object message) method is what we used.
Source
private void init() {
setLayout(new FlowLayout());
JButton button = new JButton("Open Dialog");
add(button);
button.addActionListener(event -> {
String result = JOptionPane.showInputDialog(null, "My Test Message");
System.out.println(result);
});
}

The value returned by the JOptionPane#showInputDialog() method is of type String, and is the user’s input value. When the “OK” button is pressed, an empty string will be returned, and when the “Cancel” button is pressed, a null string will be returned.
showMessageDialog
Several methods are overloaded, but here the showMessageDialog(Component parentComponent, Object message) method is what we used.
Source
private void init() {
setLayout(new FlowLayout());
JButton button = new JButton("Open Dialog");
add(button);
button.addActionListener(event -> {
JOptionPane.showMessageDialog(null, "My Test Message");
});
}

The return value of the JOptionPane#showMessageDialog() method is defined to be void. So it does not receive a value.
That’s all. I hope this is helpful to you.