Java Swing – running on EDT

A good rule is that all operations (access/updates/…) should happen on the EDT. There are a few exceptions mentioned in the javadoc (certain methods of certain classes), but they are so hard to remember that it is easier to stick to the ‘do everything on the EDT’ approach. Exceptions will not be raised (luckily, JavaFX … Read more

How can I catch Event Dispatch Thread (EDT) exceptions?

The EDT exception handler doesn’t use Thread.UncaughtExceptionHandler. Instead, it calls a method with the following signature: public void handle(Throwable thrown); Add that to MyExceptionHandler, and it should work. The “documentation” for this is found in EventDispatchThread, which is a package-private class in java.awt. Quoting from the javadoc for handleException() there: /** * Handles an exception … Read more

Is the Swing repaint() method still safe to use outside the EDT in Java 7+?

This is the official reference: Swing’s Threading Policy In general Swing is not thread safe. All Swing components and related classes, unless otherwise documented, must be accessed on the event dispatching thread. And the repaint method does not “document otherwise”. To doubly reassure you that you do not need to look any further than an … Read more

Why does setSelected on JCheckBox lose effect?

It is a known bug as acknowledged by Oracle Bug ID:6924233 The JOptionPane apparently causes another event to be generated with a check box value = false. The recommended fix is to instantiate the JOptionPane using invokeLater. Submitted On 09-MAR-2010 The change is in the BasicButtonListener – Method focusLost() In 1.6.0_18 it is … ButtonModel … Read more