Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set locale properly to a JFileChooser

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.

like image 453
Charliemops Avatar asked Jul 03 '13 10:07

Charliemops


2 Answers

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);
    }
}
like image 107
kleopatra Avatar answered Sep 28 '22 00:09

kleopatra


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.

like image 34
Charliemops Avatar answered Sep 27 '22 23:09

Charliemops