Skip to content

Commit

Permalink
Set the Look & Feel on the EDT
Browse files Browse the repository at this point in the history
To avoid potential deadlocks such as the one described at
apposed/jaunch#50 (comment)
  • Loading branch information
ctrueden committed May 21, 2024
1 parent 1317dc5 commit e4c3f4f
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions src/main/java/org/scijava/ui/swing/laf/SwingLookAndFeelService.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.FlatLightLaf;

import java.awt.EventQueue;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -112,14 +114,16 @@ public void setLookAndFeel(final String lookAndFeel) {
if (factories.containsKey(lookAndFeel)) {
// This L+F has a dedicated factory.
final LookAndFeel laf = factories.get(lookAndFeel).get();
try {
UIManager.setLookAndFeel(laf);
}
catch (final UnsupportedLookAndFeelException exc) {
attemptToRecover();
throw new IllegalArgumentException(//
"Invalid look and feel: " + lookAndFeel, exc);
}
EventQueue.invokeLater(() -> {
try {
UIManager.setLookAndFeel(laf);
}
catch (final UnsupportedLookAndFeelException exc) {
attemptToRecover();
throw new IllegalArgumentException(//
"Invalid look and feel: " + lookAndFeel, exc);
}
});
}
else {
// No dedicated factory; check for a registered L+F with a matching name.
Expand All @@ -128,23 +132,25 @@ public void setLookAndFeel(final String lookAndFeel) {
// If a L+F was found, use it; otherwise assume the argument is a class.
final String className = info == null ? lookAndFeel : info.getClassName();

try {
UIManager.setLookAndFeel(className);
}
catch (ClassNotFoundException | InstantiationException
| IllegalAccessException | UnsupportedLookAndFeelException exc)
{
attemptToRecover();
throw new IllegalArgumentException(//
"Invalid look and feel: " + lookAndFeel, exc);
}
EventQueue.invokeLater(() -> {
try {
UIManager.setLookAndFeel(className);
}
catch (ClassNotFoundException | InstantiationException
| IllegalAccessException | UnsupportedLookAndFeelException exc)
{
attemptToRecover();
throw new IllegalArgumentException(//
"Invalid look and feel: " + lookAndFeel, exc);
}

// Update all existing Swing windows to the new L+F.
FlatLaf.updateUI();

// Persist L+F setting for next time.
if (prefs != null) prefs.put(getClass(), LAF_PREF_KEY, lookAndFeel);
});
}

// Update all existing Swing windows to the new L+F.
FlatLaf.updateUI();

// Persist L+F setting for next time.
if (prefs != null) prefs.put(getClass(), LAF_PREF_KEY, lookAndFeel);
}

/**
Expand Down

0 comments on commit e4c3f4f

Please sign in to comment.