Reputation: 124
I am working on a trivia game to learn Java and am having trouble differentiating between when a user presses the (usually red) cancel/X button on the top of their window and pressing the Cancel button from JOptionPane.showInputDialog's Ok and Cancel options. The following is my code where questions are asked and the answers are read in:
public int askQuestion() {
JOptionPane optionPane = new JOptionPane("Here is a question", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
String guess = optionPane.showInputDialog(null, question, "Heres a test", JOptionPane.QUESTION_MESSAGE);
if (guess == null){
return ANSWER_CLOSED;
} else {
if (checkAnswer(guess))
return ANSWER_CORRECT;
else
return ANSWER_INCORRECT;
}
}
ANSWER_CLOSED is returned by both the cancel and close buttons, is there a way to differentiate between the two? Thanks
Upvotes: 2
Views: 211
Reputation: 1559
No, there isn't. That particular API does not provide enough granular information about what happened to know whether they clicked the button or clicked the "X" on the window.
You can of course make your own UI and be able to tell that way, or possibly subclass JOptionPane and add a new showInputDialog method with more knowledge of the internal state of the window.
Upvotes: 1