Sign-up now. Start my free, unlimited access.
Java’s JOptionPane provides a simple way to read input from the user and display information back. There are many helpful functions included in the JOptionPane’s list of methods, including: Each of these functions is relatively straight forward with the exception of the JOptionPane’s showOptionDialog method. To help demystify the most complicated of these, let’s take a look at a few different JOptionPane showOptionDialog examples. The JOptionPane’s showOptionDialog method requires an array of text Strings. Each element in the array is displayed as a clickable button. When a button is clicked, the windows closes and the array index of the element selected is returned to the program. Here’s a simple showOptionDialog example. An image of the dialog box this code generates can be seen below. These are the two most important lines of code from the showOptionDialog example above: String[] options = { "rock", "paper", "scissors" }; var selection = JOptionPane.showOptionDialog(null, "Select one:", "Let's play a game!", 0, 3, null, options, options[0]);The array named options defines the three fields to display in the dialog box. The array is then passed as the seventh of eight parameters required by the JOptionPane’s showOptionDialog method. When the dialog box appears, the names of the elements in the array appear as clickable buttons. The array index of the button clicked is returned to the program. The fact that the showOptionDialog function takes eight parameters can make it both intimidating to use and difficult to understand. Here are the eight fields the showOptionDialog method defines, along with how they can be used in your application:
To simplify the use of the JOptionPane’s showOptionDialog method, every parameter except the value of the array can be set to null or zero and the dialog box will function properly: String[] options = { "brownie", "pie", "cake" }; int response = JOptionPane.showOptionDialog(null, null, null, 0, 0, null, options, null);Furthermore, the JOptionPane class defines enums for the optionType and messageType parameters, as demonstrated below: String[] options = { "brownie", "pie", "cake" }; int response = JOptionPane.showOptionDialog(null, null, null, JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, null);To make the most use of the JOptionsPane’s showOptionDialog method, a developer should take advantage of all of the available parameters. The following advanced JOptionsPane’s showOptionDialog example takes advantage of each parameter, adding a red JFrame upon which to display the dialog box, and an image icon featuring the Java mascot duke. An image of the JOptionPane this code creates can be seen below. import java.awt.Color; import javax.swing.*; public class JOptionPaneExample { public static void main(String[] args) { String[] options = { "brownie", "pie", "cake" }; int x = JOptionPane.showOptionDialog(null, null, null, JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, null); JFrame frame = new JFrame(); frame.getContentPane().setBackground(Color.blue); frame.setBounds(300, 300, 500, 350); frame.setVisible(true); int dessert = JOptionPane.showOptionDialog(frame, "Which dessert would you like?", "Select a dessert", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, new ImageIcon("C:\\_tools\\java-duke-sm.jpg"), options, options[0]); if (dessert == 0) { JOptionPane.showMessageDialog(frame, "You chose a brownie."); } if (dessert == 1) { JOptionPane.showMessageDialog(frame, "You chose pie."); } if (dessert == 2) { JOptionPane.showMessageDialog(frame, "You chose cake."); } } }When the JOptionPane showMessageDialog() function runs, the dialog box appears on top of a blue frame, with Java’s Duke mascot as the icon. Garnering user input in Java programs can be difficult, but the JOptionPane’s showMessageDialog() makes it possible to create engaging, visual input forms that will improve the experience of your users.
SearchAppArchitecture SearchSoftwareQuality SearchCloudComputing SearchSecurity SearchAWS |