I'm having a little problem when I change Locale at runtime.
The Goal
I have to change the Locale of my application language according to a configuration file.
This locale does not necessarily be the same of the host/OS locale nor the JVM default locale.
Moreover, I can't modify user.language
when I call the application. Then, I must do that at runtime.
The Problem
Summarizing my code, I read the configuration file and get the different options (including locale). After that, I initialize the application environment according to these configured options.
After, I build my frame and starts the application life-cycle.
public static void main(String[] args) {
File fichier;
Ini ini; //Ini4J object
Modele modele = new Modele(); //My Model class: it stores configuration and other stuff
try {
fichier = new File(Modele.CONFIGURATION);
ini = new Ini(fichier);
modele.setLocaleLang(ini.get(Modele.LOCALE, Modele.LANG, String.class));
// read more options
} catch(InvalidFileFormatException e) {
// exception processing
} catch (IOException e) {
// exception processing
} finally {
ini = null;
fichier = null;
}
// More code
JComponent.setDefaultLocale(modele.getLocaleLang());
// More initialization code
MyFrame fenetre = new MyFrame(modele);
fenetre.visualiser();
}
Well, during the life-cycle, you can open files. Obviously, I use a JFileChooser for that issue:
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jfc.setFileFilter(modele.FILTRE_OUVRIR);
jfc.showOpenDialog(null);
All the file chooser it's setted with the configured locale, but the type selector doesn't change. The following image shows the problem (OS Locale: es_ES, configured locale: fr_FR):
As you can see, in the combobox "Fichiers de type": the option is showed in Spanish instead of French.
Colud someone explain me the problem? Is something wrong in my code? Could be a problem due to I'm using a file filter?
I thank you for any suggestion.
Looks like a bug in the acceptAllFilter used by BasicFileChooserUI, it doesn't lookup the localized version of the text as it should:
This default is used if your model returns null. The only way out (that I see) is to let the model return a filter that does-the-right-thing, like f.i.:
Looking for more information I've found this previous question.
There is explained that it's a possible bug and you must change it "manually", modifying the UIManager before change the locale. It means, add the following line:
And after change locale.