How to implement a file filter
I have a JFileChooser that I would like to filter to only see txt files. I have looked at the API and the java tutorial, but I can't figure out how to make it work in my own code. How would I do this?
publicclass BillingCheckextends JPanelimplements ActionListener{
JFileChooser fc;
public BillingCheck()throws IOException{
fc =new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
}
publicvoid actionPerformed(ActionEvent e){
if (e.getSource() == openButton){
int returnVal = fc.showOpenDialog(BillingCheck.this);
if (returnVal == JFileChooser.APPROVE_OPTION){
File file = fc.getSelectedFile();
}
}
thanks
well, it was supposed to be right after file chooser in the constructor, it looked like this
FileFilter filter = new FileFilter();
filter.addExtension("txt");
filter.setDescription("Text Files");
chooser.setFileFilter(filter);
but it was complaining about how FileFilter is abstract and can't be instantiated
Well that seems to be a class of your own making. The only Java one that is close is an abstract class so that won't be working.
Anyway see this [url=http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html#fileview]http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html#fileview[/url]
> and I've been studying the swing tutorial. I'm just
> having a hard time understanding it. I didn't even
> know filefilter was abstract.
> learn something new everyday....
>
> by the way, what methods am i invoking that don't
> exist-- they all are working fine
filter.addExtension("txt");
filter.setDescription("Text Files");