- [Tutorial] JOptionPane: easy input, output with dialog boxes
- Java Syntax: Building an Input Dialog Box - ThoughtCo
- JOptionPane (Java Platform SE 7 )
This table lists examples that use JOptionPane or JDialog. To find other examples that use dialogs, see the example lists for progress bars , color choosers , and file choosers.
[Tutorial] JOptionPane: easy input, output with dialog boxes
If you re designing a custom dialog, you need to design your dialog s API so that you can query the dialog about what the user chose. For example, CustomDialog has a getValidatedText method that returns the text the user entered.
I am quite familiar with creating a dialog using JDialog and with using JOptionPane to show various simple dialogs that don't need a "full" dialog. JOptionPane says that the constructor's Object message argument can be an array with several objects (components?) in it. I can create a showInputDialog with two prompts (labels) and two text fields and it will show the four components properly. My problem is get the input from both text fields: is there a way to get 7 inputs from a JOptionPane showInputDialog?
JLabel lblTitle = new JLabel("Enter the Book's title")
JTextField tfTitle = new JTextField()
JLabel lblPrice = new JLabel("Enter the Book's price")
//JTextField tfPrice = the default text field that JOptionPane provides
Java Syntax: Building an Input Dialog Box - ThoughtCo
The JOptionPane constructors do not include this argument. Instead, you specify the parent frame when you create the JDialog that contains the JOptionPane , and you use the JDialog setLocationRelativeTo method to set the dialog position.
JOptionPane (Java Platform SE 7 )
Because the user can type anything into the text field, you might want to check the returned value and ask the user to try again if it is invalid. Another approach is to create a custom dialog that validates the user-entered data before it returns. See for an example of validating data.
Besides setting the property change listener, the following code also calls the JDialog s setDefaultCloseOperation method and implements a window listener that handles the window close attempt properly. If you do not care to be notified when the user closes the window explicitly, then ignore the bold code.
If you do not care to limit the user s choices, you can either use a form of the showInputDialog method that takes fewer arguments or specify null for the array of objects. In the Java look and feel, substituting null for possibilities results in a dialog that has a text field and looks like this:
JOptionPane s icon support lets you easily specify which icon the dialog displays. You can use a custom icon, no icon at all, or any one of four standard JOptionPane icons (question, information, warning, and error). Each look and feel has its own versions of the four standard icons. The following figure shows the icons used in the Java (and Windows) look and feel.
As you've seen in these few examples, there are several message types you can select from when display an input dialog. Here's a quick list of the message types that are available to you:
Even when you use JOptionPane to implement a dialog, you re still using a JDialog behind the scenes. The reason is that JOptionPane is simply a container that can automatically create a JDialog and add itself to the JDialog s content pane.