Set locale properly to a JFileChooser
Asked Answered
R

3

5

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

enter image description here

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.

Renaerenaissance answered 3/7, 2013 at 10:52 Comment(0)
C
5

Looks like a bug in the acceptAllFilter used by BasicFileChooserUI, it doesn't lookup the localized version of the text as it should:

// BasicFileChooserUI
/**
 * Returns the default accept all file filter
 */
public FileFilter getAcceptAllFileFilter(JFileChooser fc) {
    return acceptAllFileFilter;
}

// buggy acceptAllFilter: doens't respect locale
protected class AcceptAllFileFilter extends FileFilter {

    public AcceptAllFileFilter() {
    }

    public boolean accept(File f) {
        return true;
    }

    public String getDescription() {
        return UIManager.getString("FileChooser.acceptAllFileFilterText");
    }
}

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

protected class AcceptAllFileFilter extends FileFilter {

    private Locale locale;

    public AcceptAllFileFilter(Locale locale) {
        this.locale = locale;
    }

    @Override
    public boolean accept(File f) {
        return true;
    }

    @Override
    public String getDescription() {
        return UIManager.getString("FileChooser.acceptAllFileFilterText", locale);
    }
}
Colorado answered 3/7, 2013 at 11:26 Comment(1)
I've just found the bug explanation. Thanks so much for your answer, it's a cleaner solution than mine. I'll mark yours as correct!Renaerenaissance
R
1

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:

UIManager.put("FileChooser.acceptAllFileFilterText",
               UIManager.get( "FileChooser.acceptAllFileFilterText", 
                               modele.getLocaleLang()));

And after change locale.

Renaerenaissance answered 3/7, 2013 at 11:26 Comment(0)
K
1

I did stumble across this in a program too and for me it was to enough to define the defaultLocale inside the main method.

Looks like this:

public static void main (String args []){

        Locale.setDefault(Locale.ENGLISH);

Before calling anything from Swing

Kinelski answered 14/1, 2021 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.