From 3e9c9c90662dae483ce7b75f65fcb63fa1e63a90 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Tue, 12 Jul 2022 12:13:19 +0200 Subject: [PATCH] execute `FlatLaf.initialize()` and `uninitialize()` only for current laf For NetBeans GUI builder, which invokes `FlatLaf.initialize()` multiple times for preview, but never invokes `FlatLaf.uninitialize()`. (https://github.com/apache/netbeans/issues/4231) --- .../src/main/java/com/formdev/flatlaf/FlatLaf.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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 95f76a30..6f2c4186 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java @@ -232,6 +232,15 @@ public abstract class FlatLaf @Override public void initialize() { + // do not initialize if this is not the current look and feel + // This is only necessary for special Laf usage. E.g. in GUI builders, + // which may use multiple Lafs and may invoke this method directly. + // This avoids that listeners and factories are installed multiple times. + // In case of the NetBeans GUI builder, which does not invoke uninitialize(), + // this also avoids that listeners stay registered in the system. + if( UIManager.getLookAndFeel() != this ) + return; + if( SystemInfo.isMacOS ) initializeAqua(); @@ -303,6 +312,10 @@ public abstract class FlatLaf @Override public void uninitialize() { + // do not uninitialize if this is not the current look and feel + if( UIManager.getLookAndFeel() != this ) + return; + // remove desktop property listener if( desktopPropertyListener != null ) { Toolkit toolkit = Toolkit.getDefaultToolkit();