How to make FileFilter in java?

后端 未结 9 2140
清歌不尽
清歌不尽 2020-12-05 02:07

like in title how to make filter to .txt files?

i wrote something like this but it has error :(

 private void jMenuItem1ActionPerformed(java.awt.even         


        
9条回答
  •  粉色の甜心
    2020-12-05 02:29

    You are going wrong here:

    int retval = chooser.showOpenDialog(null); 
    public boolean accept(File directory, String fileName) {`
    return fileName.endsWith(".txt");`
    }
    

    You first show the file chooser dialog and then apply the filter! This wont work. First apply the filter and then show the dialog:

    public boolean accept(File directory, String fileName) {
            return fileName.endsWith(".txt");
    }
    int retval = chooser.showOpenDialog(null);
    

提交回复
热议问题