Set locale properly to a JFileChooser

2019-03-30 09:52发布

问题:

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.

回答1:

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


回答2:

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.