Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 contains 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.

like image 951
Alberto Avatar asked Apr 20 '11 11:04

Alberto


1 Answers

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));

        }
    }
like image 127
kleopatra Avatar answered Oct 09 '22 21:10

kleopatra