Your playTurn
method runs the code on the EDT, cause of this line javax.swing.SwingUtilities.invokeLater(new Runnable() {
, which makes your application GUI unresponsive as GUI-changing code must generally be run on the EDT
. Since your buttons won't change from your GUI, once the loop starts, it might just loop forever.
By running the code in another Thread
, you won't freeze your GUI. I'm guessing, since you don't provide much informations on the rest of your code, that you might have to change the way you handle things once your loop is done.
Edit from comments : Since you don't want playTurn
to return, don't use a thread within it and make sure playTurn
is not running on the EDT
. Your playTurn
method will return after creating and making a new Thread
run the code.
You might want to try dong it like this :
Runnable code = new Runnable() {
@Override
public void run() {
for (String turn : controller.getTurns()) {
playTurn(turn);
}
}
};
if (!SwingUtilities.isEventDispatchThread()) {
code.run();
} else {
new Thread(code).start();
}
To make sure you don't run the code on the EDT
. That way, playTurn
doesn't return until the loop condition is met, the GUI
stays responsive.
public void playTurn(String turn) {
if (controller.givePlayers().contains(turn)) {
while (!turnFinished) {
if (!button1.isEnabled() && !button1.isEnabled() && !button1.isEnabled() && !button1.isEnabled()) {
turnFinished = true;
}
}
} else {
deletePlayer(turn);
}
}
Doing this might have you change a few things more.
The idea is to make the call to a new Thread
where you don't want it/need it to wait for the code being run in a new Thread
to end to continue.