The problem with your code is when that the loadImage method executes on Event Dispatch Thread (EDT) that is used by the Swing GUI engine. While your method is executing, Swing can not update the GUI. The actionPerformed method is executed on the EDT.
For more information go to this site and lookup: Event Dispatch Thread
https://docs.oracle.com/javase/tutor...ybigindex.html
Save that link as it has lots of good documentation.
The solution is for the code in loadImage to be broken up into pieces: first set the GUI and start another task to do the work off of the EDT
When that task is done, change the GUI to report. That all can be done with the SwingWorker class.
Here is an example of what your loadImage could look like:
static boolean oneTime = true;
public static void loadImage(int angle) {
System.out.println("thread="+Thread.currentThread().getName()); // first main, then AWT-EventQueue-0
log("loadImage - 40. - loadImage started");
// Show waitLabel
if(oneTime) {
imagePanel.add(waitLabel, BorderLayout.CENTER);
oneTime = false;
}
waitLabel.setVisible(true);
log("loadImage - 20. - waitLabel added to imagePanel");
imagePanel.revalidate();
imagePanel.repaint();
log("loadImage - 41. - waitLabel show");
class DoWork extends SwingWorker<Void, Void> {
@Override
public Void doInBackground() {
System.out.println("dIBg thread="+Thread.currentThread().getName());
rotateButton.setEnabled(false); // do not allow while doing work
// Pause for 5 seconds
try {
log("loadImage - 42. - pause started");
Thread.sleep(5000); // if ON the EST - hangs the GUI updating and button response
log("loadImage - 43. - pause complete");
} catch (InterruptedException e) {
e.printStackTrace();
log("loadImage - 44. - error during pause: " + e.getMessage());
}
return null;
}
@Override
public void done() {
System.out.println("done thread="+Thread.currentThread().getName());
rotateButton.setEnabled(true); // allow when done
// Hide waitLabel
waitLabel.setVisible(false);
imagePanel.revalidate();
imagePanel.repaint();
log("loadImage - 45. - waitLabel hidden");
}
};
(new DoWork()).execute();
}
To see what thread the code is executing on, insert this statement:
System.out.println("thread="+Thread.currentThread( ).getName());