Skip to content

Commit 0f08dd4

Browse files
committed
Set the Look & Feel on the EDT
To avoid potential deadlocks such as the one described at apposed/jaunch#50 (comment)
1 parent 93e963b commit 0f08dd4

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

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

Lines changed: 30 additions & 24 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+
3840
import java.util.Arrays;
3941
import java.util.HashMap;
4042
import java.util.Map;
@@ -112,14 +114,16 @@ public void setLookAndFeel(final String lookAndFeel) {
112114
if (factories.containsKey(lookAndFeel)) {
113115
// This L+F has a dedicated factory.
114116
final LookAndFeel laf = factories.get(lookAndFeel).get();
115-
try {
116-
UIManager.setLookAndFeel(laf);
117-
}
118-
catch (final UnsupportedLookAndFeelException exc) {
119-
attemptToRecover();
120-
throw new IllegalArgumentException(//
121-
"Invalid look and feel: " + lookAndFeel, exc);
122-
}
117+
EventQueue.invokeLater(() -> {
118+
try {
119+
UIManager.setLookAndFeel(laf);
120+
}
121+
catch (final UnsupportedLookAndFeelException exc) {
122+
attemptToRecover();
123+
throw new IllegalArgumentException(//
124+
"Invalid look and feel: " + lookAndFeel, exc);
125+
}
126+
});
123127
}
124128
else {
125129
// No dedicated factory; check for a registered L+F with a matching name.
@@ -128,23 +132,25 @@ public void setLookAndFeel(final String lookAndFeel) {
128132
// If a L+F was found, use it; otherwise assume the argument is a class.
129133
final String className = info == null ? lookAndFeel : info.getClassName();
130134

131-
try {
132-
UIManager.setLookAndFeel(className);
133-
}
134-
catch (ClassNotFoundException | InstantiationException
135-
| IllegalAccessException | UnsupportedLookAndFeelException exc)
136-
{
137-
attemptToRecover();
138-
throw new IllegalArgumentException(//
139-
"Invalid look and feel: " + lookAndFeel, exc);
140-
}
135+
EventQueue.invokeLater(() -> {
136+
try {
137+
UIManager.setLookAndFeel(className);
138+
}
139+
catch (ClassNotFoundException | InstantiationException
140+
| IllegalAccessException | UnsupportedLookAndFeelException exc)
141+
{
142+
attemptToRecover();
143+
throw new IllegalArgumentException(//
144+
"Invalid look and feel: " + lookAndFeel, exc);
145+
}
146+
147+
// Update all existing Swing windows to the new L+F.
148+
FlatLaf.updateUI();
149+
150+
// Persist L+F setting for next time.
151+
if (prefs != null) prefs.put(getClass(), LAF_PREF_KEY, lookAndFeel);
152+
});
141153
}
142-
143-
// Update all existing Swing windows to the new L+F.
144-
FlatLaf.updateUI();
145-
146-
// Persist L+F setting for next time.
147-
if (prefs != null) prefs.put(getClass(), LAF_PREF_KEY, lookAndFeel);
148154
}
149155

150156
/**

0 commit comments

Comments
 (0)