Skip to content

Commit c11aa93

Browse files
committed
SwingLookAndFeelService: fail more gracefully
When initLookAndFeel cannot set the L&F, log a warning rather than crashing. See imagej/imagej-matlab#28.
1 parent bdd5170 commit c11aa93

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import javax.swing.UIManager.LookAndFeelInfo;
4646
import javax.swing.UnsupportedLookAndFeelException;
4747

48+
import org.scijava.log.LogService;
4849
import org.scijava.plugin.Parameter;
4950
import org.scijava.plugin.Plugin;
5051
import org.scijava.prefs.PrefService;
@@ -68,6 +69,9 @@ public class SwingLookAndFeelService extends AbstractService implements
6869
@Parameter(required = false)
6970
private PrefService prefs;
7071

72+
@Parameter(required = false)
73+
private LogService log;
74+
7175
/** Mapping from look-and-feel name to an associated factory. */
7276
private Map<String, Supplier<LookAndFeel>> factories;
7377

@@ -80,9 +84,19 @@ public class SwingLookAndFeelService extends AbstractService implements
8084
*/
8185
public void initLookAndFeel() {
8286
// Set the L+F to match the user setting, or "FlatLaf Light" initially.
83-
final String laf = prefs == null ? null : //
87+
final String lafPref = prefs == null ? null : //
8488
prefs.get(getClass(), LAF_PREF_KEY);
85-
setLookAndFeel(laf == null ? FlatLightLaf.NAME : laf);
89+
final String laf = lafPref == null ? FlatLightLaf.NAME : lafPref;
90+
try {
91+
setLookAndFeel(laf);
92+
}
93+
catch (final IllegalArgumentException exc) {
94+
// If something goes wrong, no worries -- just log the exception.
95+
if (log != null) {
96+
log.warn("Could not set Look & Feel '" + laf + "'");
97+
log.debug(exc);
98+
}
99+
}
86100
}
87101

88102
/**

0 commit comments

Comments
 (0)