Question

I'm trying to debug an issue where I'm using a JColorChooser. I'm trying to set the local, but it keeps reverting back to the System's Locale despite what UIManager, Locale.setDefault, or JColorChooser.setLocale I set it to.

The locale that I'm setting the JColorChooser differs from the System locale. The rest of my applet is using the requested UI.

I've tried the following:

 JColorChooser colors = new JColorChooser();
 colors.setLocale([spanish]);
 Locale.setLocale([spanish]);
 UIManager.getDefaults().setDefaultLocale([spanish]); 

However, none of those options changes the component.

Was it helpful?

Solution

EDIT

as I mentioned earlier (below :-), JColorChooser (and most other localizable pre-fabricated components) locale support is suboptimal (aka: buggy). Setting its locale property directly simply doesn't work. Per-application setting worksforme (my system default is German), though:

Locale.setDefault(new Locale("es"));

Do so very early in the application code, best before creating any component, gives the best chances to get the expected non-system localized texts.

Original answer (having ranted a bit)

Localization support in Swing is half-hearted, to put it mildly.

Part of the problem is an impedance mismatch between the locale resolve mechanism between AWT vs Swing: the former resolves up the parent chain, expecting most of the children to have a null locale property, the latter explicitly sets the default as returned by JComponent.getDefaultLocale() very early in its life (in JComponent constructor). The other part of the problem is that a setLocale fires a change event as appropriate .. but nobody listens. That has dramatic consequences for pre-fabricated containers like f.i. JColorChooser: the ui delegate should update the locale property of all children but simply does nothing. (It's easy to overlook - SwingX components like datePicker had the same problem, hopefully fixed now ;-)

What is even worse: for some reason, the ui doesn't even respect the default as set on JComponent ...

So, the only way to make it completely respect any locale seems to be to set the default locale on Locale and then restore it afterwards (if you want to have only the color chooser in that locale, which I don't think is what you want :-)

    Locale old = Locale.getDefault();
    Locale.setDefault(new Locale("es"));
    JColorChooser chooser = new JColorChooser(Color.RED);
    Locale.setDefault(old);

ahhh .. all panels (DefaultXXChooserPanel) installed by the ui are buggy in that they get the localized text by

   // wrong:
   UIManager.getString(somekey)

instead of

   // correct
   UIManager.getString(somekey, appropriateLocale) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top