Hello I have a tile (JLabel) on a Grid Layout Panel I call this tile.setBorder(BorderFactory.createEmptyBorder()); to update the Border of the tile then I call a lengthy method, but the image is updated after the lengthy method is done. I want the Border adjustment to be done and image updated on screen fully before calling the method, how do I do this?
Asked
Active
Viewed 51 times
2 Answers
1
Try with SwingUtilities.invokeLater()
to make sure that EDT is initialized properly.
Please have a look at below post to know more about this.
Sample code:
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// any GUI related code will fall here
tile.setBorder(BorderFactory.createEmptyBorder());
// do not add any lengthy method call here
}
});
For more info read comments below.
-
If the issue is still there then please share your sample code to analyze it. – Braj May 16 '14 at 20:16
-
1Actually you have this in reverse. The problem is the "lengthy method" IS executing on the EDT. You don't want the code to execute on the EDT, since that is preventing the GUI from repainting itself. – camickr May 16 '14 at 21:08
-
@camickr Yes you are absolutely right. Thanks again. – Braj May 16 '14 at 21:11
-
Although I had to put the GUI code outside the run() body – Karim El Sheikh May 16 '14 at 21:13
-
@KarimElSheikh, `Although I had to put the GUI code outside the run() body` - that is not correct. You still don't understand the problem. All updates to the GUI code MUST be done on the EDT. The long running task should NOT be on the EDT. Read the link I gave you. – camickr May 16 '14 at 23:44
0
but the image is updated after the lengthy method is done.
That is because the long running task is blocking the Event Dispatch Thread and preventing the GUI from repainting itself until the task is finished running. The solution is to use a separate Thread for the task.
Read the section from the Swing tutorial on Concurrency for more information and examples. You may find the SwingWorker
more convenients that creating your own Thread.

camickr
- 321,443
- 19
- 166
- 288