I'm doing a project to the university and I have a JOptionPane.showInputDialog that asks your name and another one that has 2 radio buttons. The thing is that I can leave it empty and the game continues. I wanted to stay still until you put a name on it and choose one of the two radio buttons. Which means, its obligatory to answer to those things.
Asked
Active
Viewed 272 times
0
-
This will be easier if you create your own custom dialog box with both the name input area and the radio buttons. When one of the buttons is clicked you can check for a non-null name entry in the text box. – swingMan May 24 '15 at 16:53
-
That would be much simpler but i have requirements on the evaluation, i need them to be separated. Sorry – Black Miriam May 24 '15 at 16:58
1 Answers
0
Not sure if i fully understand the question. Maybe a do-while can help?
String name = null;
do{
name = JOptionPane.showInputDialog(...);
}while(name == null || name.isEmpty());
This will force the user to enter a name, if the user clicks cancel or the X, the message will just re-appear. (If name == null, name.isEmpty() won't be called, avoiding NullPointerException).
If you want the program to exit if the name is null, try something like:
String name = null;
do{
name = JOptionPane.showInputDialog(...);
//Exits the program if the name is null,
//you can also use a "break;" here and handle the exit after the loop
if(name == null) System.exit(0);
}while(name.isEmpty());

GoranSH
- 96
- 5
-
I can't do that because then I need the String name, to later tell the name in other message input. If it's closed, it's no longer recognized. – Black Miriam May 24 '15 at 16:55