0

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

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) *"I have to replace JFileChooser with FileDialog."* .. why? 2) Is your question *"How to X?"*? If so, add it as an [edit] to the question. If not, think of a specific question and do the same (edit). – Andrew Thompson Nov 21 '16 at 05:37
  • As indicated in the question. The filenamefilter is not applying the filter in the above example I have provided. Further, I wanted to use standard FileDialog for Windows/Mac..If user is using windows then the save dialog should windows ui and if user is using mac then the dialog look and feel should be mac based save dialog – Shariq Qamar Nov 21 '16 at 06:04
  • *"The filenamefilter is not applying the filter"* That's a problem, not a question. **Questions** that might arise from that problem would be along the lines of *"Why doesn't it work?"*, *"How to make it work?"*, *"Is this the correct approach, or is there a better one?"* - note the `?` at the end of an inquiring statement. – Andrew Thompson Nov 21 '16 at 06:52
  • And we are here to discuss solutions to the problem and I have stated the problem clearly. If you don't have answer then please don't pretend to reply – Shariq Qamar Nov 21 '16 at 23:12
  • If you don't like my comments, feel free to [report them](http://meta.stackexchange.com/questions/66444/how-can-i-report-a-specific-bad-user). – Andrew Thompson Nov 21 '16 at 23:32

1 Answers1

4

According to the FileDialog#setFilenameFilter JavaDoc:

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

So this is expected behaviour.

Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34