Forum Stats

  • 3,740,424 Users
  • 2,248,254 Discussions
  • 7,861,246 Comments

Discussions

Should UIManager fire propertyChangeEvents here?

843807
843807 Member Posts: 46,582
edited Feb 11, 2010 2:41PM in Swing
I have a java desktop application that behaves badly after the user changes the display properties under MS-Windows.
The situation is this:
Start the application.
Right-click on the Windows desktop, bring up the Display Properties dialog, and change the theme, color scheme, font size, etc. Click apply. A tree in my app that extends JTree now paints incorrectly. There are probably other issues as well, but this is an obvious starting point.

After some investigating, I came to the conclusion that I need to call:
SwingUtilities.updateComponentTreeUI(component);
when things like this change, and the way to know that things like this have changed is to register a PropertyChangeListener on the UIManager.

So to start out, I created a simple test app, with this code in the constructor:

UIManager.addPropertyChangeListener(new PropertyChangeListener()
{
public void propertyChange( PropertyChangeEvent e )
{
System.out.println( "UIManager propertyChange " + e.getPropertyName());
}
});

I then ran the app (in NetBeans), changed display properties in Windows, and got nothing printed out.
However, if in response to a button push, I change the LookAndFeel programmatically:

if (!flipFlop_)
{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
flipFlop_ = true;
}
else
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
flipFlop_ = false;
}

When I do that, I see this printed out:

UIManager propertyChange lookAndFeel

But I need to detect that UI settings have changed due to actions outside of my app, maybe outside of the JRE? Shouldn't the UIManager be firing something here? Or is there some other static object that I need to register with? I see that a PropertyChangeListener can be added to the Toolkit object, but it requires the name of the property, and there may be many properties and I don't know the names of them.

Comments

  • aterai
    aterai Member Posts: 223 Bronze Badge
    edited Jan 29, 2010 2:04PM
    Not sure, but I think [Windows Desktop Property Support|http://java.sun.com/javase/6/docs/technotes/guides/swing/1.4/w2k_props.html] is what you are looking for.
    import java.awt.*;
    import javax.swing.*;
    import java.beans.*;
    public class Test {
      public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
          public void run() { createAndShowGUI(); }
        });
      }
      public static void createAndShowGUI() {
        try{
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }catch(Exception e) {
          e.printStackTrace();
        }
        Toolkit.getDefaultToolkit().addPropertyChangeListener(
            "win.frame.activeCaptionColor", new PropertyChangeListener() {
          public void propertyChange(PropertyChangeEvent evt) {
            System.out.println(evt.getPropertyName());
          }
        });
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.setSize(320,240);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
      }
    }
  • 843807
    843807 Member Posts: 46,582
    That looks like it will work, although registering for individual "win.frame.xxx" property change events is a bit tedious and disappointing.
  • 843807
    843807 Member Posts: 46,582
    edited Feb 10, 2010 10:42AM
    I once created LookAndFeels that adapted to Windows XP theme changes. Listening for these two properties seemed to be sufficient, neither of which seems to be in the link posted previously. . I don't recall why I needed to listen for both, just one or the other may be sufficient, but that code's so old I'm not going back :)
    protected static final String STYLE_DESKTOP_PROPERTY = "win.xpstyle.colorName";
    protected static final String THEMEACTIVE_DESKTOP_PROPERTY = "win.xpstyle.themeActive";
    These are listened for on the Toolkit.

    Edited by: BoBear2681 on Feb 10, 2010 3:41 PM

    Looking a little further, it appears that winxp.xpstyle.themeActive is fired when XP theming support is toggled (e.g. from the XP look to the Windows Classic look and vice versa), whereas win.xpstyle.colorName is fired when the XP style's color scheme is changed.
  • 843807
    843807 Member Posts: 46,582
    Try the following code
    try {
                UIManager.setLookAndFeel("LookAndFeelClass");   // or any change made in UI
                SwingUtilities.updateComponentTreeUI(this);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(MailClientFrame.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(MailClientFrame.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(MailClientFrame.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnsupportedLookAndFeelException ex) {
                Logger.getLogger(MailClientFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
  • darrylburke
    darrylburke Member Posts: 18,007
    Subhadip. wrote:
    Try the following code
    which has nothing to do with the question asked here.

    db
This discussion has been closed.