Skip to content

Commit 8f69d63

Browse files
committed
When updating the Look & Feel, always use the EDT
1 parent 4dddf65 commit 8f69d63

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

‎src/main/java/org/scijava/ui/swing/laf/SwingLookAndFeelService.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import com.formdev.flatlaf.FlatLaf;
3636
import com.formdev.flatlaf.FlatLightLaf;
3737

38+
import java.awt.EventQueue;
39+
import java.lang.reflect.InvocationTargetException;
3840
import java.util.Arrays;
3941
import java.util.HashMap;
4042
import java.util.Map;
@@ -107,6 +109,25 @@ public void initLookAndFeel() {
107109
* @throws IllegalArgumentException If the given look and feel is unknown.
108110
*/
109111
public void setLookAndFeel(final String lookAndFeel) {
112+
if (!EventQueue.isDispatchThread()) {
113+
// Note: Changing the L+F off the EDT races with Swing's own layout
114+
// and painting, which observe half-installed UI delegates and crash.
115+
try {
116+
EventQueue.invokeAndWait(() -> setLookAndFeel(lookAndFeel));
117+
}
118+
catch (final InvocationTargetException exc) {
119+
final Throwable cause = exc.getCause();
120+
if (cause instanceof RuntimeException) throw (RuntimeException) cause;
121+
if (cause instanceof Error) throw (Error) cause;
122+
throw new RuntimeException(cause);
123+
}
124+
catch (final InterruptedException exc) {
125+
Thread.currentThread().interrupt();
126+
throw new RuntimeException(exc);
127+
}
128+
return;
129+
}
130+
110131
if (factories == null) initFactories();
111132

112133
if (factories.containsKey(lookAndFeel)) {

0 commit comments

Comments
 (0)