Forcing Java to refresh the Java Swing GUI
If you start your AI game from an actionPerformed()
, it is executed in EDT thread. You should move your logic (and your sleep()
's outside of EDT thread by starting a new Thread
to allow Swing to repaint UI properly and post updates to UI as following:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
myUI.update(); // repaint(), etc. according to changed states
}
});
Also consider use of javax.swing.SwingWorker
, javax.swing.Timer
and take a look at Concurrency in Swing.
Related videos on Youtube
CNevin561
Updated on June 04, 2022Comments
-
CNevin561 almost 2 years
Ive coded two Computers to play each other in Reversi, however when they play each other. The board only updates after the game has finished.
From some googling around I know its has something to do the AWT Event Thread, but I still have no idea how to force the JFrame to refresh.
My function works by changing the icons and then calling revalidate and repaint. Any pointers would be wonderful.
-
gigadot over 12 yearsDoes it display properly when you play with your program? If so, instead of refrshing the graphic, have you tried to emulate the click event so that it is like computer is clicking on the board?
-
CNevin561 over 12 yearsUmm, i didnt think of that. But i dont know if it will work. The GUI is updated by a separate method called
updateBoard()
, which changes the Board based on the internal representation. Though something to do with the while loop means it doesnt update till it ends -
gigadot over 12 yearsSo you didn't use Model-View-Controller pattern to implement you GUI? How does your application wait for user response? is it done using a loop or event? It is pssoible that your application GUI is frozen due to heavy CPU load, that's why it doesn't get updated util the computation ends. What happen if you make the next player pause for a few seconds? if the GUI get updated properly then my assumption above should be corect.
-
CNevin561 over 12 yearsno its not MVC unfortuately. This is the first GUI that ive done and i was already mostly finished when I learnt about MVC. The gamme does work with having 1 or more user, but when its sorely AI it doesnt update, probably because theres no pause. Its a while(gameNotOver) do .....
-
gigadot over 12 yearsso try making the thread to sleep inside that loop.
-
CNevin561 over 12 yearsI did by calling
Thread.sleep(1000)
but it didnt work. -
gigadot over 12 yearswas it being called after the board update?
-
-
barjak over 12 years-1 : even if others threads are busy, the GUI would stay responsive if the EDT is not blocked. There are 99% chances that its logic happens in the EDT.
-
Jason about 8 yearsThis answer is probably the correct one given the limited information provided by the OP. However, I have noticed a similar behavior in my app and I'm not running my CPU intensive code on the EDT. So it's possible that the EDT (or other low level Java code) isn't getting the CPU time it needs to draw because the application is already taking up so much CPU time.