Shariq Qamar Shariq Qamar - 15 days ago 8
Java Question

Filter not working in FileDialog

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

}

Answer

According to the FileDialog#setFilenameFilter JavaDoc:

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

So this is expected behaviour.

Comments