UI properties does not contain some keys

前端 未结 2 1611
梦谈多话
梦谈多话 2021-01-13 05:08

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

UIManager.getString(\"OptionPane.okButtonText\")

that returns the string

相关标签:
2条回答
  • 2021-01-13 05:12

    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 讨论(0)
  • 2021-01-13 05:21

    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));
    
            }
        }
    
    0 讨论(0)
提交回复
热议问题