Easy methods to use the JOptionPane’s showOptionDialog technique
Java’s JOptionPane supplies a easy method to learn enter from the person and show data again.
There are various useful capabilities included within the JOptionPane’s listing of strategies, together with:
- showInputDialog
- showMessageDialog
- showConfirmDialog
- showOptionDialog
Every of those capabilities is comparatively straight ahead except the JOptionPane’s showOptionDialog technique.
JOptionPane showOptionDialog examples
To assist demystify essentially the most difficult of those, let’s check out a number of totally different JOptionPane showOptionDialog examples.
The JOptionPane’s showOptionDialog technique requires an array of textual content Strings.
Every ingredient within the array is displayed as a clickable button. When a button is clicked, the home windows closes and the array index of the ingredient chosen is returned to this system.
Right here’s a easy showOptionDialog instance. A picture of the dialog field this code generates may be seen beneath.
import javax.swing.*; public class ShowOptionDialogExample public static void principal(String[] args) /* Easy JOptionPane ShowOptionDialogJava instance */ String[] choices = "rock", "paper", "scissors" ; var choice = JOptionPane.showOptionDialog(null, "Choose one:", "Let's play a recreation!", 0, 3, null, choices, choices[0]); if (choice == 0) JOptionPane.showMessageDialog(null, "You selected rock!"); if (choice == 1) JOptionPane.showMessageDialog(null, "You selected paper."); if (choice == 2) JOptionPane.showMessageDialog(null, "You selected scissors!");
Java’s showOptionDialog technique defined
These are the 2 most vital traces of code from the showOptionDialog instance above:
String[] choices = "rock", "paper", "scissors" ; var choice = JOptionPane.showOptionDialog(null, "Choose one:", "Let's play a recreation!", 0, 3, null, choices, choices[0]);
The Swing JOptionPane showOptionDialog can take customized enter from the person.
The array named choices defines the three fields to show within the dialog field.
The array is then handed because the seventh of eight parameters required by the JOptionPane’s showOptionDialog technique.
When the dialog field seems, the names of the weather within the array seem as clickable buttons.
The array index of the button clicked is returned to this system.
The showOptionDialog’s many arguments
The truth that the showOptionDialog operate takes eight parameters could make it each intimidating to make use of and obscure.
Listed here are the eight fields the showOptionDialog technique defines, together with how they can be utilized in your software:
- The primary parameter is the parentComponent, which may be set to null if the dialog field is to be rendered independently
- The second parameter is the immediate to be displayed contained in the dialog field
- The third parameter is the textual content to be displayed within the title of the dialog field
- The fourth parameter is the optionType, which would be the quantity 0,1,2, or 3, to specify whether or not sure, no or cancel buttons will seem
- The fifth parameter is the messageType, which would be the quantity 0,1 or 2 or 3 to specify whether or not an error, information, warning, query or default message icon will seem
- The sixth parameter is an non-obligatory picture icon to show within the message field
- The seventh parameter is the array of choices to make use of
- The both parameter is the array object to pick out because the default worth
Easy showOptionDialog defaults
To simplify the usage of the JOptionPane’s showOptionDialog technique, each parameter besides the worth of the array may be set to null or zero and the dialog field will operate correctly:
String[] choices = "brownie", "pie", "cake" ; int response = JOptionPane.showOptionDialog(null, null, null, 0, 0, null, choices, null);
Moreover, the JOptionPane class defines enums for the optionType and messageType parameters, as demonstrated beneath:
String[] choices = "brownie", "pie", "cake" ; int response = JOptionPane.showOptionDialog(null, null, null, JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, choices, null);
Superior JOptionsPane’s showOptionDialog instance
To take advantage of use of the JOptionsPane’s showOptionDialog technique, a developer ought to make the most of the entire accessible parameters.
The next superior JOptionsPane’s showOptionDialog instance takes benefit of every parameter, including a pink JFrame upon which to show the dialog field, and a picture icon that includes the Java mascot duke. A picture of the JOptionPane this code creates may be seen beneath.
import java.awt.Coloration;
import javax.swing.*;
public class JOptionPaneExample
public static void principal(String[] args)
String[] choices = "brownie", "pie", "cake" ;
int x = JOptionPane.showOptionDialog(null, null, null,
JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
null, choices, null);
JFrame body = new JFrame();
body.getContentPane().setBackground(Coloration.blue);
body.setBounds(300, 300, 500, 350);
body.setVisible(true);
int dessert = JOptionPane.showOptionDialog(body,
"Which dessert would you want?", "Choose a dessert",
JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
new ImageIcon("C:_toolsjava-duke-sm.jpg"),
choices, choices[0]);
if (dessert == 0)
JOptionPane.showMessageDialog(body, "You selected a brownie.");
if (dessert == 1)
JOptionPane.showMessageDialog(body, "You selected pie.");
if (dessert == 2)
JOptionPane.showMessageDialog(body, "You selected cake.");
When the JOptionPane showMessageDialog() operate runs, the dialog field seems on high of a blue body, with Java’s Duke mascot because the icon.

When absolutely parameterized, the JOptionPane showOptionDialog shows on a body with a customized icon.
Garnering person enter in Java applications may be troublesome, however the JOptionPane’s showMessageDialog() makes it attainable to create partaking, visible enter varieties that can enhance the expertise of your customers.