How to remove JFileChooser default accept all file filter?

A

Allen

Accept all file filter is default for JFileChooser. I want to remove
it. How to do it?
 
A

Andrew Thompson

John said:
You don't. You replace it.

How?

My quick experiments indicate that ..
setFileFilter(FileFilter)
... is a poorly named ..
addAndSelectFileFilter(FileFilter)

To actually *remove* the initial file filter,
I needed to do this..

JFileChooser jfc = new JFileChooser();
// the first, only, and selected filter is 'All Files'
jfc.removeChoosableFileFilter(
jfc.getFileFilter() );
// ...now add the filter to an empty list
jfc.setFileFilter(new FileTypeFilter(".java")) ;

Is there some other method, I missed?

Andrew T.
 
J

John W. Kennedy

Andrew said:
How?

My quick experiments indicate that ..
setFileFilter(FileFilter)
... is a poorly named ..
addAndSelectFileFilter(FileFilter)

To actually *remove* the initial file filter,
I needed to do this..

JFileChooser jfc = new JFileChooser();
// the first, only, and selected filter is 'All Files'
jfc.removeChoosableFileFilter(
jfc.getFileFilter() );
// ...now add the filter to an empty list
jfc.setFileFilter(new FileTypeFilter(".java")) ;

Is there some other method, I missed?

If you must, and are on 1.3 or better, setAcceptAllFileFilterUsed(false).

But every user who wants to damn-well see the "all" list will curse you
for doing so.
 
A

Andrew Thompson

John said:
....
If you must, and are on 1.3 or better, setAcceptAllFileFilterUsed(false).

Aaah.. the marvels of ..'modern' technology
( I got away with that, didn't I? ;).
But every user who wants to damn-well see the "all" list will curse you
for doing so.

I'm glad you brought that up. It is not something I
have ever attempted (till I saw your first post on it -
which got me curious).

I tend to agree that even if the end-user has no point in
openning a particular file-type in my application, I will
still offer the 'All Files' filter - so they can use it for
navigation (I am used to what most of my more common
directories *look* like in a file filter - it would be a nuisance
to have the contents 'changing' according to what flavor
of files the application likes).

The 'path of least surprise' might suggest it is better
to leave the 'All Files' filter in place.

Andrew T.
 
Joined
Sep 18, 2013
Messages
1
Reaction score
0
Just put All Files as last option

Instead of completely remove the All Files option, try adding to the end by first removing it. It gets automatically added as the first entry, not my favorite either.

JFileChooser fc = new javax.swing.JFileChooser();
fc.setAcceptAllFileFilterUsed(false);
fc.addChoosableFileFilter(new FileNameExtensionFilter("Batch Files (*.bat)", "bat"));
fc.addChoosableFileFilter(new FileNameExtensionFilter("Text Files (*.txt)", "txt"));
fc.setAcceptAllFileFilterUsed(true);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top