From 9b465cb550bd9a43000dc6d18e5d4a658a03fc16 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Mon, 9 Aug 2021 19:27:49 +0200 Subject: [PATCH] Accent color: added `FlatLaf.setExtraDefaults()` for easy setting accent color at runtime (issue #233) --- .../java/com/formdev/flatlaf/FlatLaf.java | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java index 9ff1ff0a..0b29bcdb 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java @@ -86,6 +86,7 @@ public abstract class FlatLaf private static final String DESKTOPFONTHINTS = "awt.font.desktophints"; private static List customDefaultsSources; + private Map extraDefaults; private String desktopPropertyName; private String desktopPropertyName2; @@ -464,7 +465,12 @@ public abstract class FlatLaf } protected Properties getAdditionalDefaults() { - return null; + if( extraDefaults == null ) + return null; + + Properties properties = new Properties(); + properties.putAll( extraDefaults ); + return properties; } private void initResourceBundle( UIDefaults defaults, String bundleName ) { @@ -779,6 +785,38 @@ public abstract class FlatLaf customDefaultsSources.remove( folder ); } + /** + * Gets extra UI defaults; or {@code null}. + * + * @since 1.6 + */ + public Map getExtraDefaults() { + return extraDefaults; + } + + /** + * Sets extra UI defaults, which are only used when setting up the application look and feel. + * E.g. using {@link UIManager#setLookAndFeel(LookAndFeel)} or {@link #setup(LookAndFeel)}. + *

+ * The extra defaults are useful for smaller additional defaults that may change. + * E.g. accent color. Otherwise FlatLaf properties files should be used. + * See {@link #registerCustomDefaultsSource(String)}. + *

+ * The keys and values are strings in same format as in FlatLaf properties files. + *

+ * Sample that setups "FlatLaf Light" theme with red accent color: + *

{@code
+	 * FlatLaf laf = new FlatLightLaf();
+	 * laf.setExtraDefaults( Collections.singletonMap( "@accentColor", "#f00" ) );
+	 * FlatLaf.setup( laf );
+	 * }
+ * + * @since 1.6 + */ + public void setExtraDefaults( Map extraDefaults ) { + this.extraDefaults = extraDefaults; + } + private static void reSetLookAndFeel() { EventQueue.invokeLater( () -> { LookAndFeel lookAndFeel = UIManager.getLookAndFeel();