How to make JFileChooser show every type of files except .huff files

87 Views Asked by At

I'm working on a Java (Swing) program implementing Huffman algorithm. It takes a file and compresses it to filename.huff I have to make the JFileChooser show every file in the system but not the ones I have compressed that have .huff extension

I haven't tried any code because I don't know how can I do that. But I know how to make a FileFilter that shows only specific type of Files:

FileFilter filter = new FileNameExtensionFilter("Huffman File","huff");
JFileChooser j = new JFileChooser(System.getProperty("user.dir"));
j.setFileFilter(filter);
int returnVal = j.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
   txtBrowse.setText(j.getSelectedFile().getAbsolutePath());
}

So, the question is how to make FileFilter displays all type of files except .huff files?

1

There are 1 best solutions below

0
Andrew Thompson On

Use a javax.swing.filechooser.FileFilter which:

..is an abstract class used by JFileChooser for filtering the set of files shown to the user. See FileNameExtensionFilter for an implementation that filters using the file name extension.
A FileFilter can be set on a JFileChooser to keep unwanted files from appearing in the directory listing. For an example implementation of a simple file filter, see yourJDK/demo/jfc/FileChooserDemo/ExampleFileFilter.java. For more information and examples see How to Use File Choosers, a section in The Java Tutorial.

Note the bold part which was obtained after a few moments glancing at the methods of JFileChooser This is something you should do before asking questions on SO.

Here is the effect of implementing one using .txt as the file to exclude:

enter image description here