4141import javax .swing .UIManager .LookAndFeelInfo ;
4242import javax .swing .UnsupportedLookAndFeelException ;
4343
44+ import com .formdev .flatlaf .FlatLightLaf ;
45+ import com .formdev .flatlaf .FlatDarkLaf ;
46+ import com .formdev .flatlaf .FlatIntelliJLaf ;
47+ import com .formdev .flatlaf .FlatDarculaLaf ;
48+
4449import org .scijava .display .Display ;
4550import org .scijava .display .DisplayService ;
4651import org .scijava .log .LogService ;
@@ -131,8 +136,15 @@ public void run() {
131136
132137 protected void initLookAndFeel () {
133138 final String lafClass = UIManager .getLookAndFeel ().getClass ().getName ();
139+ LookAndFeelInfo [] lookAndFeels = UIManager .getInstalledLookAndFeels ();
140+
141+ // Make UIManager aware of FlatLaf look and feels, as needed
142+ if (!isRegistered (lookAndFeels , FlatLightLaf .NAME )) FlatLightLaf .installLafInfo ();
143+ if (!isRegistered (lookAndFeels , FlatDarkLaf .NAME )) FlatDarkLaf .installLafInfo ();
144+ if (!isRegistered (lookAndFeels , FlatDarculaLaf .NAME )) FlatDarculaLaf .installLafInfo ();
145+ if (!isRegistered (lookAndFeels , FlatIntelliJLaf .NAME )) FlatIntelliJLaf .installLafInfo ();
146+ lookAndFeels = UIManager .getInstalledLookAndFeels (); // retrieve updated list
134147
135- final LookAndFeelInfo [] lookAndFeels = UIManager .getInstalledLookAndFeels ();
136148 final ArrayList <String > lookAndFeelChoices = new ArrayList <>();
137149 for (final LookAndFeelInfo lafInfo : lookAndFeels ) {
138150 final String lafName = lafInfo .getName ();
@@ -149,6 +161,15 @@ protected void initLookAndFeel() {
149161
150162 // -- Helper methods --
151163
164+ /** Assesses whether lookAndFeels contains the laf associated with lafName*/
165+ private boolean isRegistered (final LookAndFeelInfo [] lookAndFeels , final String lafName ) {
166+ for (final LookAndFeelInfo lafInfo : lookAndFeels ) {
167+ if (lafInfo .getName ().equals (lafName ))
168+ return true ;
169+ }
170+ return false ;
171+ }
172+
152173 /** Tells all known Swing components to change to the new Look & Feel. */
153174 private void refreshSwingComponents () {
154175 // TODO: Change this hacky logic to call a clean UIService API
@@ -202,6 +223,41 @@ private DisplayService displayService() {
202223 return getContext ().service (DisplayService .class );
203224 }
204225
226+ /**
227+ * Sets the application look and feel.
228+ * <p>
229+ * Useful for setting up the look and feel early on in application startup
230+ * routine (e.g., through a macro or script)
231+ * </p>
232+ *
233+ * @param lookAndFeel the look and feel. Supported values include "FlatLaf
234+ * Light", "FlatLaf Dark", "FlatLaf Darcula", "FlatLaf
235+ * IntelliJ", and JVM defaults
236+ * ("javax.swing.plaf.metal.MetalLookAndFeel", etc.)
237+ */
238+ public static void setupLookAndFeel (final String lookAndFeel ) {
239+ switch (lookAndFeel ) {
240+ case FlatLightLaf .NAME :
241+ FlatLightLaf .setup ();
242+ return ;
243+ case FlatDarkLaf .NAME :
244+ FlatDarkLaf .setup ();
245+ return ;
246+ case FlatDarculaLaf .NAME :
247+ FlatDarculaLaf .setup ();
248+ return ;
249+ case FlatIntelliJLaf .NAME :
250+ FlatIntelliJLaf .setup ();
251+ return ;
252+ default :
253+ try {
254+ UIManager .setLookAndFeel (lookAndFeel );
255+ } catch (final Exception ex ) {
256+ ex .printStackTrace ();
257+ }
258+ }
259+ }
260+
205261 // -- Deprecated methods --
206262
207263 @ Deprecated
0 commit comments