FlatLaf icon indicating copy to clipboard operation
FlatLaf copied to clipboard

Changes in runtime.

Open andrestubbe opened this issue 1 year ago • 3 comments

Hello folks,

i want to change certain values after runtime. Before my JFrame is visible all changes are fine, but after setVisible(true) nothing happens. Is there a way to change certain values like...

    UIManager.put("RootPane.background", Color.red);
    UIManager.put("TitlePane.background", Color.red);
    UIManager.put("TitlePane.inactiveBackground", Color.red);

... after initialisation? I saw the wonder first in the demo. What am i missing?

i Just found https://github.com/JFormDesigner/FlatLaf/issues/847. Am i right there? In my case i use JOGL for most of the Window and your FlatLightLaf serves as titleBar manipulation.

Screenshot 2024-06-25 105520

Sincerly & Thx

andrestubbe avatar Jun 25 '24 08:06 andrestubbe

The values in UIManager are used when creating components and are then stored in component UI delegates. So you need to update UI delegates in that case.

You can use FlatLaf.updateUI() to update all UI delegates in your app. Or SwingUtilities.updateComponentTreeUI( w ); to update a single window.

DevCharly avatar Jun 25 '24 10:06 DevCharly

Thank you so much, Karl. I'll wrote a small demo for that. What didn't work for me so far is to change the RootPane.background. probably i am missing a certain keyword. the titlebar should be RED.

package examples;

import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.FlatLightLaf;

import javax.swing.*;
import java.awt.*;

public class Example_FlatLightLaf_Change {

    private final JFrame frame;

    public static void main(String[] args) {
        new Example_FlatLightLaf_Change();
    }

    public Example_FlatLightLaf_Change() {
        try {
            UIManager.setLookAndFeel(new FlatLightLaf());
        } catch (Exception ex) {
            System.err.println("Failed to initialize LaF");
        }
        UIManager.put("flatlaf.useWindowDecorations", true);
        UIManager.put("RootPane.background", Color.BLACK);

        frame = new JFrame("FlatLAF Change Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(640, 480);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        change_theme();
    }

    public void change_theme() {

        // ???
        UIManager.put("RootPane.background", Color.RED);

        FlatLaf.updateUI();
        SwingUtilities.updateComponentTreeUI( this.frame);

    }
}

andrestubbe avatar Jun 27 '24 06:06 andrestubbe

I found it via Bing Copilot.

JRootPane rootPane = frame.getRootPane();
rootPane.putClientProperty("JRootPane.titleBarBackground", Color.RED);

Hooray :)

andrestubbe avatar Jun 27 '24 08:06 andrestubbe