Reputation:
I was wondering if it is possible to put multiple inputs in JOptionPane.showInputDialog and then get the user input and if the user has given a wrong input for one of the questions then provide them with a error, asking them to re-enter that specific data again.
For example, in the input I want questions like;
So instead of have a JOptionPane.showInputDialog for each question like this:
int timeout;
do {
String timeoutinputbyuser = JOptionPane.showInputDialog("How many times have to been out? between 1-10.");
timeout = Integer.parseInt(timeoutinputbyuser);
} while (timeout < 1 || timeout > 10);
I want to have all the questions in one and provide a suitable error if the user gets any question wrong.
Upvotes: 1
Views: 16923
Reputation: 392
Try this
JTextField field1 = new JTextField();
JTextField field2 = new JTextField();
JTextField field3 = new JTextField();
JTextField field4 = new JTextField();
JTextField field5 = new JTextField();
Object[] message = {
"Input value 1:", field1,
"Input value 2:", field2,
"Input value 3:", field3,
"Input value 4:", field4,
"Input value 5:", field5,
};
int option = JOptionPane.showConfirmDialog(parent, message, "Enter all your values", JOptionPane.OK_CANCEL_OPTION);
if (option == JOptionPane.OK_OPTION)
{
String value1 = field1.getText();
String value2 = field2.getText();
String value3 = field3.getText();
String value4 = field4.getText();
String value5 = field5.getText();
}
Upvotes: 1
Reputation: 168815
No, the input dialog only accepts a single input area.
Put the components in a JPanel
and display it in a JOptionPane.showMessageDialog(..)
. Note that you can then have better components:
JSpinner
for selecting a number.JRadioButton
objects in a ButtonGroup
for the choice of 3..Upvotes: 6