I understand the frustration. Every time you modify a Swing component you should do so using invokeLater (or invokeAndWait). The problem arises if several threads decide to do something to the same Swing component at the same time. Unexpected results could occur. So if each thread simply schedules that task to run on the EDT, they are guaranteed that the event will occur sequentially with no inter-thread interference. But it is important to not do things that would take too long because the gui could become unresponsive. So do most of the work outside of the EDT and then update the gui accordingly. Here is an example that demonstrates the issue(s).
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EDTDemo implements ActionListener {
public static void main(String[] args) {
new EDTDemo().start();
}
public void start() {
// Simple bookkeeping to create a frame and button
// and center it on screen.
JFrame frame = new JFrame();
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(200, 200));
JButton button = new JButton("Click");
button.setPreferredSize(new Dimension(100, 100));
button.addActionListener(this);
frame.add(button);
frame.pack();
frame.setLocationRelativeTo(null); // center on screen
frame.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
JButton b = (JButton) ae.getSource();
b.setBackground(Color.RED);
SwingUtilities.invokeLater(() ->
{
try {
// simulates other processing (exaggerated of course)
TimeUnit.SECONDS.sleep(10);
}
catch (InterruptedException ie) {
}
});
}
}
Without the invokeLater, that task would tie up the EDT and the button wouldn't be painted for 10 seconds. But this is still a problem. The 10 second task is also scheduled to run on the EDT. So even though the button gets painted immediately, tje program will be unresponsive for 10 seconds. When one clicks to close the window, that event gets scheduled to run on the EDT *after* the long task runs.
Note that setting the color was done in the EDT because it was done during the processing of an event -- the button click. But it was just updating a setting to be painted later so it didn't need to be scheduled separately.
Regards,
Jim