Filter not working in FileDialog

一笑奈何 提交于 2019-12-13 07:52:19

问题


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:


According to the FileDialog#setFilenameFilter JavaDoc:

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

So this is expected behaviour.



来源:https://stackoverflow.com/questions/40713398/filter-not-working-in-filedialog

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