Class FlatLaf

All Implemented Interfaces:
Serializable
Direct Known Subclasses:
FlatDarkLaf, FlatLightLaf, FlatPropertiesLaf, IntelliJTheme.ThemeLaf

public abstract class FlatLaf extends BasicLookAndFeel
The base class for all Flat LaFs.
See Also:
  • Field Details

  • Constructor Details

    • FlatLaf

      public FlatLaf()
  • Method Details

    • setup

      public static boolean setup(LookAndFeel newLookAndFeel)
      Sets the application look and feel to the given LaF using UIManager.setLookAndFeel(javax.swing.LookAndFeel).
      Since:
      1.2
    • install

      @Deprecated public static boolean install(LookAndFeel newLookAndFeel)
      Deprecated.
      use setup(LookAndFeel) instead; this method will be removed in a future version
    • installLafInfo

      public static void installLafInfo(String lafName, Class<? extends LookAndFeel> lafClass)
      Adds the given look and feel to the set of available look and feels.

      Useful if your application uses UIManager.getInstalledLookAndFeels() to query available LaFs and display them to the user in a combobox.

    • getID

      public String getID()
      Returns the look and feel identifier.

      Syntax: "FlatLaf - ${theme-name}"

      Use UIManager.getLookAndFeel().getID().startsWith( "FlatLaf" ) to check whether the current look and feel is FlatLaf.

      Specified by:
      getID in class LookAndFeel
    • isDark

      public abstract boolean isDark()
    • isLafDark

      public static boolean isLafDark()
      Checks whether the current look and feel is dark.
    • getSupportsWindowDecorations

      public boolean getSupportsWindowDecorations()
      Returns whether FlatLaf supports custom window decorations. This depends on the operating system and on the used Java runtime.

      This method returns true on Windows 10/11 (see exception below) and on Linux, otherwise returns false.

      Returns also false on Windows 10/11 if FlatLaf native window border support is available (requires Windows 10/11).

      In these cases, custom decorations are enabled by the root pane. Usage of JFrame.setDefaultLookAndFeelDecorated(boolean) or JDialog.setDefaultLookAndFeelDecorated(boolean) is not necessary.

      Overrides:
      getSupportsWindowDecorations in class LookAndFeel
    • isNativeLookAndFeel

      public boolean isNativeLookAndFeel()
      Specified by:
      isNativeLookAndFeel in class LookAndFeel
    • isSupportedLookAndFeel

      public boolean isSupportedLookAndFeel()
      Specified by:
      isSupportedLookAndFeel in class LookAndFeel
    • getDisabledIcon

      public Icon getDisabledIcon(JComponent component, Icon icon)
      Overrides:
      getDisabledIcon in class LookAndFeel
    • initialize

      public void initialize()
      Overrides:
      initialize in class BasicLookAndFeel
    • uninitialize

      public void uninitialize()
      Overrides:
      uninitialize in class BasicLookAndFeel
    • initializeAqua

      private void initializeAqua()
      Initialize Aqua LaF on macOS, which is required for using Mac screen menubar. (at least on Java 8, since 9 it seems to work without it)

      This loads the native library "osxui" and initializes JRSUI. Because both are not unloaded/uninitialized, Aqua LaF is initialized only once.

    • getDefaults

      public UIDefaults getDefaults()
      Overrides:
      getDefaults in class BasicLookAndFeel
    • applyAdditionalDefaults

      void applyAdditionalDefaults(UIDefaults defaults)
    • getLafClassesForDefaultsLoading

      protected List<Class<?>> getLafClassesForDefaultsLoading()
    • getAdditionalDefaults

      protected Properties getAdditionalDefaults()
    • initResourceBundle

      private void initResourceBundle(UIDefaults defaults, String bundleName)
    • initFonts

      private void initFonts(UIDefaults defaults)
    • initDefaultFont

      private void initDefaultFont(UIDefaults defaults)
    • createCompositeFont

      static FontUIResource createCompositeFont(String family, int style, int size)
    • createActiveFontValue

      public static UIDefaults.ActiveValue createActiveFontValue(float scaleFactor)
      Since:
      1.1
    • initIconColors

      public static void initIconColors(UIDefaults defaults, boolean dark)
      Adds the default color palette for action icons and object icons to the given UIDefaults.

      This method is public and static to allow using the color palette with other LaFs (e.g. Windows LaF). To do so invoke: FlatLaf.initIconColors( UIManager.getLookAndFeelDefaults(), false ); after UIManager.setLookAndFeel( ... );.

      The colors are based on IntelliJ Platform Action icons and Noun icons

      These colors may be changed by IntelliJ Platform themes.

    • putAATextInfo

      private void putAATextInfo(UIDefaults defaults)
    • fallbackAATextInfo

      private Object fallbackAATextInfo()
    • putDefaults

      private void putDefaults(UIDefaults defaults, Object value, String... keys)
    • getCustomDefaultsSources

      static List<Object> getCustomDefaultsSources()
    • registerCustomDefaultsSource

      public static void registerCustomDefaultsSource(String packageName)
      Registers a package where FlatLaf searches for properties files with custom UI defaults.

      This can be used to specify application specific UI defaults that override UI values of existing themes or to define own UI values used in custom controls.

      There may be multiple properties files in that package for multiple themes. The properties file name must match the used theme class names. E.g. FlatLightLaf.properties for class FlatLightLaf or FlatDarkLaf.properties for class FlatDarkLaf. FlatLaf.properties is loaded first for all themes.

      These properties files are loaded after theme and addon properties files and can therefore override all UI defaults.

      Invoke this method before setting the look and feel.

      If using Java modules, the package must be opened in module-info.java. Otherwise, use registerCustomDefaultsSource(URL).

      Parameters:
      packageName - a package name (e.g. "com.myapp.resources")
    • unregisterCustomDefaultsSource

      public static void unregisterCustomDefaultsSource(String packageName)
    • registerCustomDefaultsSource

      public static void registerCustomDefaultsSource(String packageName, ClassLoader classLoader)
      Registers a package where FlatLaf searches for properties files with custom UI defaults.

      See registerCustomDefaultsSource(String) for details.

      Parameters:
      packageName - a package name (e.g. "com.myapp.resources")
      classLoader - a class loader used to find resources, or null
    • unregisterCustomDefaultsSource

      public static void unregisterCustomDefaultsSource(String packageName, ClassLoader classLoader)
    • registerCustomDefaultsSource

      public static void registerCustomDefaultsSource(URL packageUrl)
      Registers a package where FlatLaf searches for properties files with custom UI defaults.

      See registerCustomDefaultsSource(String) for details.

      This method is useful if using Java modules and the package containing the properties files is not opened in module-info.java. E.g. FlatLaf.registerCustomDefaultsSource( MyApp.class.getResource( "/com/myapp/themes/" ) ).

      Parameters:
      packageUrl - a package URL
      Since:
      2
    • unregisterCustomDefaultsSource

      public static void unregisterCustomDefaultsSource(URL packageUrl)
      Since:
      2
    • registerCustomDefaultsSource

      public static void registerCustomDefaultsSource(File folder)
      Registers a folder where FlatLaf searches for properties files with custom UI defaults.

      See registerCustomDefaultsSource(String) for details.

      Parameters:
      folder - a folder
    • unregisterCustomDefaultsSource

      public static void unregisterCustomDefaultsSource(File folder)
    • getGlobalExtraDefaults

      public static Map<String,String> getGlobalExtraDefaults()
      Gets global extra UI defaults; or null.
      Since:
      2
    • setGlobalExtraDefaults

      public static void setGlobalExtraDefaults(Map<String,String> globalExtraDefaults)
      Sets global extra UI defaults, which are only used when setting up the application look and feel. E.g. using UIManager.setLookAndFeel(LookAndFeel) or setup(LookAndFeel).

      The global extra defaults are useful for smaller additional defaults that may change. Otherwise, FlatLaf properties files should be used. See registerCustomDefaultsSource(String).

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

      Sample that setups "FlatLaf Light" theme with white background color:

      
       FlatLaf.setGlobalExtraDefaults( Collections.singletonMap( "@background", "#fff" ) );
       FlatLightLaf.setup();
       
      Since:
      2
      See Also:
    • getExtraDefaults

      public Map<String,String> getExtraDefaults()
      Gets extra UI defaults; or null.
      Since:
      2
    • setExtraDefaults

      public void setExtraDefaults(Map<String,String> extraDefaults)
      Sets extra UI defaults, which are only used when setting up the application look and feel. E.g. using UIManager.setLookAndFeel(LookAndFeel) or setup(LookAndFeel).

      The extra defaults are useful for smaller additional defaults that may change. Otherwise, FlatLaf properties files should be used. See registerCustomDefaultsSource(String).

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

      Sample that setups "FlatLaf Light" theme with white background color:

      
       FlatLaf laf = new FlatLightLaf();
       laf.setExtraDefaults( Collections.singletonMap( "@background", "#fff" ) );
       FlatLaf.setup( laf );
       
      Since:
      2
      See Also:
    • parseDefaultsValue

      public static Object parseDefaultsValue(String key, String value, Class<?> valueType) throws IllegalArgumentException
      Parses a UI defaults value string and converts it into a binary object.

      See: https://www.formdev.com/flatlaf/properties-files/

      Parameters:
      key - the key, which is used to determine the value type if parameter valueType is null
      value - the value string
      valueType - the expected value type, or null
      Returns:
      the binary value
      Throws:
      IllegalArgumentException - on syntax errors
      Since:
      2
    • getSystemColorGetter

      public static Function<String,Color> getSystemColorGetter()
      Returns the system color getter function, or null.
      Since:
      3
    • setSystemColorGetter

      public static void setSystemColorGetter(Function<String,Color> systemColorGetter)
      Sets a system color getter function that is invoked when function systemColor() is used in FlatLaf properties files.

      The name of the system color is passed as parameter to the function. The function should return null for unknown system colors.

      Can be used to change the accent color:

      
       FlatLaf.setSystemColorGetter( name -> {
           return name.equals( "accent" ) ? Color.red : null;
       } );
       FlatLightLaf.setup();
       
      Since:
      3
    • reSetLookAndFeel

      private static void reSetLookAndFeel()
    • updateUI

      public static void updateUI()
      Update UI of all application windows immediately. Invoke after changing LaF.
    • updateUILater

      public static void updateUILater()
      Update UI of all application windows later.
    • supportsNativeWindowDecorations

      public static boolean supportsNativeWindowDecorations()
      Returns whether native window decorations are supported on current platform.

      This requires Windows 10/11, but may be disabled if running in special environments (JetBrains Projector, Webswing or WinPE) or if loading native library fails. If system property FlatSystemProperties.USE_WINDOW_DECORATIONS is set to false, then this method also returns false.

      Since:
      1.1.2
    • isUseNativeWindowDecorations

      public static boolean isUseNativeWindowDecorations()
      Returns whether native window decorations are enabled.
      Since:
      1.1.2
    • setUseNativeWindowDecorations

      public static void setUseNativeWindowDecorations(boolean enabled)
      Sets whether native window decorations are enabled.

      Existing frames and dialogs will be updated.

      Since:
      1.1.2
    • revalidateAndRepaintAllFramesAndDialogs

      public static void revalidateAndRepaintAllFramesAndDialogs()
      Revalidate and repaint all displayable frames and dialogs.

      Useful to update UI after changing TitlePane.menuBarEmbedded.

      Since:
      1.1.2
    • repaintAllFramesAndDialogs

      public static void repaintAllFramesAndDialogs()
      Repaint all displayable frames and dialogs.

      Useful to update UI after changing TitlePane.unifiedBackground, MenuItem.selectionType or Component.hideMnemonics.

      Since:
      1.1.2
    • isDisplayableFrameOrDialog

      private static boolean isDisplayableFrameOrDialog(Window w)
    • isShowMnemonics

      public static boolean isShowMnemonics()
    • showMnemonics

      public static void showMnemonics(Component c)
    • hideMnemonics

      public static void hideMnemonics()
    • equals

      public final boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public final int hashCode()
      Overrides:
      hashCode in class Object
    • registerUIDefaultsGetter

      public void registerUIDefaultsGetter(Function<Object,Object> uiDefaultsGetter)
      Registers a UI defaults getter function that is invoked before the standard getter. This allows using different UI defaults for special purposes (e.g. using multiple themes at the same time).

      The key is passed as parameter to the function. If the function returns null, then the next registered function is invoked. If all registered functions return null, then the current look and feel is asked. If the function returns NULL_VALUE, then the UI value becomes null.

      Since:
      1.6
      See Also:
    • unregisterUIDefaultsGetter

      public void unregisterUIDefaultsGetter(Function<Object,Object> uiDefaultsGetter)
      Unregisters a UI defaults getter function that was invoked before the standard getter.
      Since:
      1.6
      See Also:
    • runWithUIDefaultsGetter

      public static void runWithUIDefaultsGetter(Function<Object,Object> uiDefaultsGetter, Runnable runnable)
      Registers a UI defaults getter function that is invoked before the standard getter, runs the given runnable and unregisters the UI defaults getter function again. This allows using different UI defaults for special purposes (e.g. using multiple themes at the same time). If the current look and feel is not FlatLaf, then the getter is ignored and the given runnable invoked.

      The key is passed as parameter to the function. If the function returns null, then the next registered function is invoked. If all registered functions return null, then the current look and feel is asked. If the function returns NULL_VALUE, then the UI value becomes null.

      Example:

      
       // create secondary theme
       UIDefaults darkDefaults = new FlatDarkLaf().getDefaults();
      
       // create panel using secondary theme
       FlatLaf.runWithUIDefaultsGetter( key -> {
           Object value = darkDefaults.get( key );
           return (value != null) ? value : FlatLaf.NULL_VALUE;
       }, () -> {
           // TODO create components that should use secondary theme here
       } );
       
      Since:
      1.6
      See Also:
    • getStyleableInfos

      public static Map<String,Class<?>> getStyleableInfos(JComponent c)
      Returns information about styleable values of a component.

      This is equivalent to: ((StyleableUI)c.getUI()).getStyleableInfos(c)

      Since:
      2.5
    • getStyleableValue

      public static <T> T getStyleableValue(JComponent c, String key)
      Returns the (styled) value for the given key from the given component.

      This is equivalent to: ((StyleableUI)c.getUI()).getStyleableValue(c, key)

      Since:
      2.5
    • getPreferredFontFamily

      public static String getPreferredFontFamily()
      Returns the preferred font family to be used for (nearly) all fonts; or null.
      Since:
      3
    • setPreferredFontFamily

      public static void setPreferredFontFamily(String preferredFontFamily)
      Sets the preferred font family to be used for (nearly) all fonts.

      Note: This must be invoked before setting the application look and feel.

      Since:
      3
    • getPreferredLightFontFamily

      public static String getPreferredLightFontFamily()
      Returns the preferred font family to be used for "light" fonts; or null.
      Since:
      3
    • setPreferredLightFontFamily

      public static void setPreferredLightFontFamily(String preferredLightFontFamily)
      Sets the preferred font family to be used for "light" fonts.

      Note: This must be invoked before setting the application look and feel.

      Since:
      3
    • getPreferredSemiboldFontFamily

      public static String getPreferredSemiboldFontFamily()
      Returns the preferred font family to be used for "semibold" fonts; or null.
      Since:
      3
    • setPreferredSemiboldFontFamily

      public static void setPreferredSemiboldFontFamily(String preferredSemiboldFontFamily)
      Sets the preferred font family to be used for "semibold" fonts.

      Note: This must be invoked before setting the application look and feel.

      Since:
      3
    • getPreferredMonospacedFontFamily

      public static String getPreferredMonospacedFontFamily()
      Returns the preferred font family to be used for monospaced fonts; or null.
      Since:
      3
    • setPreferredMonospacedFontFamily

      public static void setPreferredMonospacedFontFamily(String preferredMonospacedFontFamily)
      Sets the preferred font family to be used for monospaced fonts.

      Note: This must be invoked before setting the application look and feel.

      Since:
      3