UI properties does not contain some keys
Asked Answered
A

2

5

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.

Aftermath answered 20/4, 2011 at 11:9 Comment(5)
something wrong in your code? - worksformeSigismondo
Cf. UIManager DefaultsMccammon
I add the code. @Mccammon I get the code from this site :)Aftermath
are you doing something unusual (whatever that means ;-) relative to the default locale of your system? Like having an Italian default and trying to access the English version?Sigismondo
ahh - just noticed the "iterate over the keyset": doesn't make sense, that's not where they are stored, see the clarification in my answer (and ignore my last comment here ;-)Sigismondo
S
4

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

        }
    }
Sigismondo answered 20/4, 2011 at 14:49 Comment(2)
Thanks for clarifying. I think your analysis is correct, but I'm out of votes. Localized resources do not seem to appear in the entrySet() of UIDefaults .Mccammon
This is exactly what I was looking for! Very thanks for all the answers!Aftermath
M
4

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
Mccammon answered 20/4, 2011 at 12:57 Comment(4)
But I try it on Windows 7 with the system L&F, and if I get the property with the getString method it works fine. The problem is get all the keys. I think we should get all the various ComponentClass UI and the keys int this way, but how?Aftermath
@alberto: I'm not seeing an entry for OptionPane.okButtonText on Windows 7; results added above.Mccammon
doesn't make much sense to loop through the entries, see the clarification (hopefully :-) of my answerSigismondo
@kleopatra: Aha, I think I see now; I've updated this answer to reflect my Windows 7 and Mac OS X results using your approach.Mccammon
S
4

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

        }
    }
Sigismondo answered 20/4, 2011 at 14:49 Comment(2)
Thanks for clarifying. I think your analysis is correct, but I'm out of votes. Localized resources do not seem to appear in the entrySet() of UIDefaults .Mccammon
This is exactly what I was looking for! Very thanks for all the answers!Aftermath

© 2022 - 2024 — McMap. All rights reserved.