Search code examples
javaswinglook-and-feel

Can't update look and feel on the fly


I have a program that I want to be able to switch between "day" and "night" modes, with the "night" mode having 50% gray backgrounds on everything. I'm doing this by calling UIManager.put(key, new color) for all of the "background" keys from this page:

http://alvinalexander.com/java/java-uimanager-color-keys-list

Then I use SwingUtilities.updateComponentTreeUI() in conjunction with java.awt.Window.getWindows() to get existing windows to pick up the change.

When I switch modes, newly-created windows will have the proper background, so the first part works. However, existing windows are behaving oddly: they flash to the new color for a moment, and then switch back. Thus e.g. if I start up the program in Day mode, then the main program window is effectively stuck in Day mode, and vice versa.

I attempted to make an example program. It doesn't behave exactly like my existing program, but it still behaves oddly: the look and feel can only be modified once. After that, new changes are ignored:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Frame;
import javax.swing.JLabel;
import java.awt.GridLayout;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;


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

   public Demo() {
      JFrame frame = new JFrame("Look and feel test");
      frame.setLayout(new GridLayout(3, 3));
      // Fill in non-central locations in the layout.
      // Hacky way to keep the button from monopolizing the frame
      for (int i = 0; i < 4; ++i) {
         frame.add(new JLabel(""));
      }
      JButton button = new JButton("Set new LAF");
      button.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            setNewLAF();
         }
      });
      frame.add(button);
      // Fill in non-central locations in the layout.
      for (int i = 0; i < 4; ++i) {
         frame.add(new JLabel(""));
      }
      frame.setMinimumSize(new Dimension(400, 400));
      frame.show();
      System.out.println("Initial frame background is " + frame.getBackground());
   }

   public void setNewLAF() {
      Random rng = new Random();
      Color newBackground = new Color(rng.nextInt(255),
            rng.nextInt(255), rng.nextInt(255));
      System.out.println("New color is " + newBackground);
      UIManager.put("Panel.background", newBackground);
      for (Frame f : Frame.getFrames()) {
         SwingUtilities.updateComponentTreeUI(f);
         System.out.println("Frame now has background " + f.getBackground());     
      }
   }
}

This produces output such as:

Initial frame background is com.apple.laf.AquaImageFactory$SystemColorProxy[r=238,g=238,b=238]
New color is java.awt.Color[r=243,g=209,b=134]
Frame now has background java.awt.Color[r=243,g=209,b=134]
New color is java.awt.Color[r=205,g=141,b=58]
Frame now has background java.awt.Color[r=243,g=209,b=134]
New color is java.awt.Color[r=141,g=22,b=92]
Frame now has background java.awt.Color[r=243,g=209,b=134]

Thanks for any advice you'd care to share.


Solution

  • it still behaves oddly: the look and feel can only be modified once.

    Color newBackground = new Color(rng.nextInt(255),rng.nextInt(255), rng.nextInt(255));
    

    You need to use:

    Color newBackground = new ColorUIResource(rng.nextInt(255),rng.nextInt(255), rng.nextInt(255));
    

    The updateComponentTreeUI() only replaces resources that are part of the UI. You do this by wrapping the Color in a ColorUIResource.