Set locale properly to a JFileChooser

与世无争的帅哥 提交于 2019-12-03 16:25:54

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!