In a Swing app a method should continue only after user enters a correct answer. The correct answer is stored in a String
with user answer being set by a listener to another String
. So, the code is
while (!correctAnswer.equals(currentAnswer)) {
// wait for user to click the button with the correct answer typed into the textfield
}
// and then continue
Is everything fine with this approach or would you somehow refactor it? Doesn't it impose extra penalty on CPU? Here's a somewhat similar question.
Are you new to UI programming? The reason I ask is that your answer is predicated on a procedural style of coding, which isn't what UIs are about. It tends to be event-driven.
In this case the solution is pretty easy: add an event listener (ActionListener
) to the submit button and check the result there. If its OK, go on. If not, say so and let them try again.