1

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.

  • Your JPanel should probably use [BoxLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html). – Radiodef Jan 22 '14 at 18:47

2 Answers2

6

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..
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    K, I am just coding it now, I have a rough idea of what I want but I can't seem to add errors and some other things. Give me 2 mins, just finishing up my code and I post it here. –  Jan 22 '14 at 18:06
  • @CherryW Your update is really a different question. – Radiodef Jan 22 '14 at 18:46
  • How?, this is what I wanted from the begining –  Jan 22 '14 at 18:49
  • oh, Maybe I didn't explain myself properly sorry about that. –  Jan 22 '14 at 18:50
  • 1
    I am new to java and Don't know all the options I have available –  Jan 22 '14 at 18:50
  • It often pays to describe the background of a problem, outlining the actual use-case in the initial question. Like the 'quoted area' at the top of [this question](http://stackoverflow.com/q/21142686/418556). I have to admit though, that spec. was written by me, **after** writing the code, so I had an ..unfair advantage in that I was *really* describing the content of the answer (and with considerable experience of what is and is not practical in a Swing GUI). – Andrew Thompson Jan 22 '14 at 18:56
1

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();
}
rush2sk8
  • 362
  • 6
  • 16