JFormDesigner / FlatLaf

FlatLaf - Swing Look and Feel (with Darcula/IntelliJ themes support)
https://www.formdev.com/flatlaf/
Apache License 2.0
3.42k stars 272 forks source link

Question: The correct way to change/switch between themes #847

Closed EchoEllet closed 5 months ago

EchoEllet commented 6 months ago

Hi, I would like to know more details on how to switch between themes in the recommended way, I already looked at the example (at this setTheme() method) and I'm still not sure on how to achieve that correctly, for example, when do we need to call the following functions:

FlatAnimatedLafChange.showSnapshot();

or:

FlatLaf.updateUI();
FlatAnimatedLafChange.hideSnapshotWithAnimation();

The class FlatAnimatedLafChange is from FlatLaf extras

Related issues:

Consider enabling GitHub Discussions for this repository as it's designed for questions and suggestions.

I appreciate your time and effort on this library.

DevCharly commented 5 months ago

FlatAnimatedLafChange.showSnapshot() and FlatAnimatedLafChange.hideSnapshotWithAnimation() are only needed if you want animated (e.g. fading from light to dark) theme change. It is not required. See https://www.javadoc.io/doc/com.formdev/flatlaf-extras/latest/com/formdev/flatlaf/extras/FlatAnimatedLafChange.html

What you need is UIManager.setLookAndFeel( ... ) or e.g. FlatDarkLaf.setup(), which changes the active theme (look and feel), but does not update existing components.

FlatLaf.updateUI(); is required to update existing components with the new theme.

EchoEllet commented 5 months ago

FlatAnimatedLafChange.showSnapshot() and FlatAnimatedLafChange.hideSnapshotWithAnimation() are only needed if you want animated (e.g. fading from light to dark) theme change. It is not required. See https://www.javadoc.io/doc/com.formdev/flatlaf-extras/latest/com/formdev/flatlaf/extras/FlatAnimatedLafChange.html

What you need is UIManager.setLookAndFeel( ... ) or e.g. FlatDarkLaf.setup(), which changes the active theme (look and feel), but does not update existing components.

FlatLaf.updateUI(); is required to update existing components with the new theme.

So it's okay to change the theme by calling FlatDarkLaf.setup() and FlatLaf.updateUI in runtime after setting an initial theme

Thank you once again.