Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Stronger filtering on JFileChooser  RSS feed

 
Jason Richard
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm using a JFileChooser. I'm using a FileNameExtensionFilter which is fantastic, and will let me only show files that are of type .db or whatever.

However

I don't want to show the user files that have a type of lock.db or *_trace.db for example, and I can't figure out a way to do that.

Is there a way to do that?
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there is.

The examples you find all seem to use FileNameExtensionFilter, but if you look at the API documentation you'll notice that JFileChooser.setFileFilter() actually accepts any kind of FileFilter, not just a FileNameExtensionFilter. So you can write your own subclass of FileFilter which accepts or rejects File objects according to whatever rules you have. Your rules only apply to the name of the class, as far as I can see, but the rules you implement in a FileFilter can be anything at all. You could filter out certain folders, or you could even look inside the files and accept or reject based on their contents.
 
Jason Richard
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you Paul. That was perfect. You are right, all examples have been the FileNameExtensionFilter that I could find.

I did find that I couldn't use both a FileNameExtensionFilter and a FileFilter at the same time. The Chooser would take it, but seemed to ignore one, so I just added the extensionFiltering to my FileFilter and everything worked great!

 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Richard wrote:I did find that I couldn't use both a FileNameExtensionFilter and a FileFilter at the same time. The Chooser would take it, but seemed to ignore one

A JFileChooser can have multiple filters, which the user can select from the "Files of Type" combo.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!