Possible Duplicate:
How can I wait for a button press in a loop?
I think this question has been asked before however I have not found a response that solves my situation.
I currently have a chess program (command line) that allows a user to input the coordinates of the square holding the piece that they would like to move. The beginning works like this.
boolean success;
do {
success = true;
// take in coordinates;
// perform checks such as not empty square etc;
if (/*problem found*/) {
// print error message;
success = false;
}
}
while (!success);
This works great when running in a command line because each time the loop is run, the program pauses execution to wait for the user to input the coordinates. However, now I want to implement this with a GUI using buttons and action listeners. The most convenient solution would be not to change the code apart from the input coordinates section which would be replaced with something such as,
pause program;
user presses button which sets coordinates;
when button pressed program continues;
perform checks…;
How can this be done?
Is there a method in java that says wait for button press?
This method would have to insure that other parts of the program such as painting the GUI does not stop.
I guess you don’t need a “pause”. You need to call a function when a button is clicked. You can put the action to do into a function, then call this function from the Listener’s implementations. As your example says button, you can implement ActionListener to your Frame object, then actionPerformed() function calls your function.