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

[1338 byte] By [snoboardera] at [2007-11-26 15:34:50]
# 1
Where is your FileFilter?
cotton.ma at 2007-7-8 21:52:16 > top of Java-index,Java Essentials,New To Java...
# 2

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

snoboardera at 2007-7-8 21:52:16 > top of Java-index,Java Essentials,New To Java...
# 3

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]

cotton.ma at 2007-7-8 21:52:16 > top of Java-index,Java Essentials,New To Java...
# 4
This may come as a shock, but FileFilter is abstract and cannot be instantiated. If that's not enough you're invoking methods that don't exist too. Maybe it's time to hit up the Swing tutorial? http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html
kablaira at 2007-7-8 21:52:16 > top of Java-index,Java Essentials,New To Java...
# 5
Is there anyway to do it inside of my own class though?
snoboardera at 2007-7-8 21:52:16 > top of Java-index,Java Essentials,New To Java...
# 6
> Is there anyway to do it inside of my own class> though?read the tutorial; you want to filter files on same basis you decide right? this is what the file filter class is all about; you write a subclass for it and use that.read the tutorial cotton posted.
shoopy.a at 2007-7-8 21:52:16 > top of Java-index,Java Essentials,New To Java...
# 7
> Is there anyway to do it inside of my own class> though?You will need at least one more class.
cotton.ma at 2007-7-8 21:52:16 > top of Java-index,Java Essentials,New To Java...
# 8
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
snoboardera at 2007-7-8 21:52:16 > top of Java-index,Java Essentials,New To Java...
# 9

> 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");

cotton.ma at 2007-7-8 21:52:16 > top of Java-index,Java Essentials,New To Java...
# 10
ok, well, those aren't working...
snoboardera at 2007-7-8 21:52:16 > top of Java-index,Java Essentials,New To Java...
# 11
and it works, just had to add a class thanks.
snoboardera at 2007-7-8 21:52:16 > top of Java-index,Java Essentials,New To Java...