Filter not working in FileDialog

2019-09-19 14:10发布

I have to replace JFileChooser with FileDialog. I could not Filter out the file. Following is the code. The filter is also not visible on the Save FileDialog and also user is able to save the file file with different ext. The environment is windows 10

public class ABCDialog extends JDialog {

private String fileName = StringUtils.Empty;

public ABCDialog (Frame frame,String title) {
    super(frame, title);
    .....
}

public String getFileName () {
    return fileName;
}

private String setFileName () {
    FileDialog file = new FileDialog(new Frame(), "Save File...", FileDialog.SAVE);
    final FilenameFilter filenameFilter = (dir, name) -> name.endsWith(".txt");

    file.setFilenameFilter(filenameFilter);
    file.setFile("*.txt");
    file.setVisible(true);
    return (file.getDirectory() + file.getFile());
}

@Override
public void setVisible(boolean visible) {
    ....
    fileName = setFileName();
}

}

1条回答
干净又极端
2楼-- · 2019-09-19 14:40

According to the FileDialog#setFilenameFilter JavaDoc:

Filename filters do not function in Sun's reference implementation for Microsoft Windows.

So this is expected behaviour.

查看更多
登录 后发表回答