user3211917
user3211917

Reputation:

Is it possible to put multiple input in JOptionPane.showInputDialog?

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;

  1. How many times have to been out? between 1-10.
  2. Do you like number 1 or 2 or 3?
  3. Please state for how many hours your have between 1-10 you have stop in a restaurant?
  4. and I will need to add some more at a later stage.

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

Answers (2)

rush2sk8
rush2sk8

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

Andrew Thompson
Andrew Thompson

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:

  • A JSpinner for selecting a number.
  • JRadioButton objects in a ButtonGroup for the choice of 3..

Upvotes: 6

Related Questions