开发者

UI properties does not contain some keys

I have the following problem. I need to get an UI properties:

UIManager.getString("OptionPane.okButtonText")

that returns the string "OK", and it works. However, if I iterate through the UIDefaults keyset, I never get the key "OptionPane.okButtonText". Does anyone know why it happens? I get the UIDefaults in three different way (UIManager.getDefaults(), UIManager.getLookAndFeel().getDefaults() and UIManager.getLookAndFeelDefaults()), but no one of these work.

Edit: I also find this list of properties of the class JFileChooser, that contain开发者_StackOverflows some properties that do not appear int the UIDefaults keyset. The problem is: how programmatically get all this properties?

Edit: Example of code:

UIDefaults defaults = UIManager.getDefaults();
String thekey = "OptionPane.okButtonText";
System.out.println(thekey + ": " + UIManager.getString(thekey));
for (Enumeration e = defaults.keys(); e.hasMoreElements();) {
    Object key = e.nextElement();
    System.out.println(key + ": " + defaults.get(key));
}

this code return print these properties. The key "OptionPane.okButtonText" dont appear in the output.


This could be a problem with resourceBundles: the optionPane (as well as f.i. fileChooser and other) text properties are loaded from localized bundles. They are (used to be, not entirely sure if that's still the case) internal classes under com.sun.swing.internal.plaf. Maybe something's going wrong there ...

here's the snippet that worksforme:

    String ok = "OptionPane.okButtonText";
    String text = ""; 
    text += " LAF: " + UIManager.getLookAndFeelDefaults().get(ok);
    text += " lookup: " + UIManager.get(ok);
    text += " default: " + UIManager.getDefaults().get(ok);
    System.out.println(text);

    // output, whereever I add that:
     LAF: OK lookup: OK default: OK

independent of which LAF is currently installed. My system is win/vista, my default locale de

Edit: just to clarify - the localized resources are not necessarily direct entries in the keys()/entrySet(), these are methods in Hashtable which are not overridden in UIDefaults. So while the lookup as in my snippet should always work querying the enums is wrong - the entries are not there but in some cached maps which are fed by resourceBundles.

Edit2: added the def of ok (thought that would be ... obvious after talking for several hours about that key :-)

Edit3: for further experiments, we should probably lookup a value which differs more than "OK" across Locales, f.i. cancelButtonText

Edit 4 (the very last before a major break, promised :-) - as to "how-to find all localized values" is not possible without resorting to dirty means (aka: implementation details). The only way I can think of is to look into the resourceBundles which are - assumedly - loaded, like

    import com.sun.swing.internal.plaf.basic.resources.basic;

    String cancel = "OptionPane.cancelButtonText";
    ListResourceBundle bundle = new basic();
    for (String key : bundle.keySet()) {
        if(cancel.equals(key)) {
            System.out.println(key
                    + ": " + bundle.getString(key));

        }
    }


It appears that OptionPane.okButtonText is a feature unique to Aqua available in all L&Fs, as shown using this approach that includes localized values not seen when iterating over the entrySet().

import javax.swing.UIDefaults;
import javax.swing.UIManager;

/** @see https://stackoverflow.com/questions/5729306 */
public class OptionPaneDefaults {

    public static void main(String[] args) throws Exception {
        UIManager.LookAndFeelInfo[] lfa =
            UIManager.getInstalledLookAndFeels();
        for (UIManager.LookAndFeelInfo lf : lfa) {
            UIManager.setLookAndFeel(lf.getClassName());
            UIDefaults uid = UIManager.getLookAndFeelDefaults();
            System.out.println("***"
                + " " + lf.getName()
                + " " + lf.getClassName()
                + " " + uid.size() + " entries");
            String ok = "OptionPane.okButtonText";
            String text = "";
            text += " LAF: " + UIManager.getLookAndFeelDefaults().get(ok);
            text += " lookup: " + UIManager.get(ok);
            text += " default: " + UIManager.getDefaults().get(ok);
            System.out.println(text);
        }
    }
}

Console, Mac OS X:

*** Metal javax.swing.plaf.metal.MetalLookAndFeel 636 entries
 LAF: OK lookup: OK default: OK
*** Nimbus com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel 1054 entries
 LAF: OK lookup: OK default: OK
*** CDE/Motif com.sun.java.swing.plaf.motif.MotifLookAndFeel 550 entries
 LAF: OK lookup: OK default: OK
*** Mac OS X com.apple.laf.AquaLookAndFeel 711 entries
 LAF: OK lookup: OK default: OK

Console, Windows 7:

*** Metal javax.swing.plaf.metal.MetalLookAndFeel 636 entries
 LAF: OK lookup: OK default: OK
*** Nimbus com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel 1049 entries
 LAF: OK lookup: OK default: OK
*** CDE/Motif com.sun.java.swing.plaf.motif.MotifLookAndFeel 550 entries
 LAF: OK lookup: OK default: OK
*** Windows com.sun.java.swing.plaf.windows.WindowsLookAndFeel 637 entries
 LAF: OK lookup: OK default: OK
*** Windows Classic com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel 637 entries
 LAF: OK lookup: OK default: OK
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜