Search code examples
javaswinglook-and-feel

Changing LookAndFeel while the application is running


I wanted to change the LookAndFeel by clicking on the button

public static void main(String[] args) {
    FlatDarculaLaf.setup();

    try {
        UIManager.setLookAndFeel(new FlatDarculaLaf());
    } catch (Exception e) {
        e.printStackTrace();
    }


    JFrame frame = new JFrame("Test");
    frame.setLayout(new FlowLayout());

    JButton b = new JButton("Click");
    b.setSize(10, 5);
    b.addActionListener(e -> {
        FlatLightLaf.setup();
        try {
            UIManager.setLookAndFeel(new FlatLightLaf());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    });

    frame.getContentPane().add(b);

    frame.setVisible(true);
    frame.setBounds(0, 0, 1200, 600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

but it didn't work. Is there a way to do this, or is it not possible at all?


Solution

  • Use SwingUtilities.updateComponentTreeUI(frame) after changing the UI to update the UI tree. This should update the frame and all its components