Aqeel Hussain
Aqeel Hussain

Reputation: 43

Can not get FileNameExtensionFilter to work

I can not get my JFileChooser to show only the extensions I want (text files).

JFileChooser fc = new JFileChooser();
            FileNameExtensionFilter textFilter = new FileNameExtensionFilter("Text Files","txt");
            fc.addChoosableFileFilter(textFilter);

What am I doing wrong? if I remove the filter the text files show up. Question might seem silly, but I have tried to find a solution and my code looks the same as other examples. Maybe I am just brain-locked.

Thank you for your time

I tried the suggested approach and I still have the same problem. So there must be something wrong elsewhere. I will load the class code - maybe someone can spot what I am doing wrong:

import java.awt.event.ActionEvent;

 import java.awt.event.ActionListener;
 import javax.swing.JButton;
 import javax.swing.JFileChooser;
 import javax.swing.JFrame;
 import javax.swing.filechooser.FileNameExtensionFilter;


public class ButtonListener implements ActionListener {

private JFrame fr;

public ButtonListener (JFrame frame){
    fr = frame;
}

public void actionPerformed(ActionEvent event) {
    if(event.getSource() instanceof JButton) {
        String action = event.getActionCommand();

        if (action.equals("First text")){

            JFileChooser fc = new JFileChooser();
            FileNameExtensionFilter textFilter = new FileNameExtensionFilter("Text Files","txt");
            fc.addChoosableFileFilter(textFilter);
            int returnVal = fc.showOpenDialog(fr);
            }
        else if (action.equals("Second text")){

            }
        else {
            System.out.println("Error in ButtonListener");
        }
    }

}

}

It is not finished, but it should still be able to handle then extension issues that I am having.

Upvotes: 1

Views: 11711

Answers (2)

user3743238
user3743238

Reputation:

I know your question was already answered, but just in case you didn't know... You could have also use you FileNameExtensionFilter and just do:

fc.setFileFilter(textFilter);

I was stuck on the same prob. as you for a while. ;)

Upvotes: 0

Thorn
Thorn

Reputation: 4057

Try using setFileFilter instead.

I never used FileNameExtensionFilter, but it's nice that they added this convenience class to Java 1.6

I have always just extended FileFilter and then override accept. Using my class defined below, you could then write

chooser.setFileFilter(new OpenFileFilter("txt"));

/**
 * This class defines which file types are opened (by default) by the program.
 * This file filter is used to associate a single file type (extension) with the program.
 * You could add more than one file type to the open file dialog using this class by repeatedly
 *    calling addFileFilter.
 */
import java.io.File;
import javax.swing.filechooser.*;

public class OpenFileFilter extends FileFilter {
    public String fileExt = "";
    String txtExt = ".txt";

    public OpenFileFilter() {
        this(".pxml");  //default file type extension.
    }

    public OpenFileFilter(String extension) {
        fileExt = extension;
    }

     @Override public boolean accept(File f) {
        if (f.isDirectory())
            return true;
        return  (f.getName().toLowerCase().endsWith(fileExt)); 
    }

    public String getDescription() {
        if(fileExt.equals(txtExt ))
            return  "Text Files (*" + fileExt + ")";
        else
            return ("Other File");
    }
}

Upvotes: 2

Related Questions