Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Adding FileFilters to JFileChooser

18 views
Skip to first unread message

Jason Cavett

unread,
Mar 23, 2009, 10:31:43 PM3/23/09
to
Okay, I know this should be easy, but I really cannot got this to
work.

I'm adding FileFilters (DefaultFileFilters) to a JFileChooser in the
following way:

List<DefaultFileFilter> filters = new ArrayList<DefaultFileFilter>();
filters.add(new DefaultFileFilter(".ff1", "File Filter 1"));
filters.add(new DefaultFileFilter(".ff2", "File Filter 2"));
filters.add(new DefaultFileFilter(".ff3", "File Filter 3"));

JFileChooser openDialog = new JFileChooser();
for (DefaultFileFilter filter : callback.getFileFilters()) {
openDialog.setFileFilter(filter);
}


That works, however, the last filter added is always the one that is
selected, when I want the first one selected by default. Changing
setFileFilter to setChoosableFileFilter does not fix the problem.
Adding the file filters in the reverse order does not fix the issue
(the correct filter is selected, but the filters are in the opposite
order).

This should be easy. What am I doing wrong?

Mark Space

unread,
Mar 23, 2009, 10:42:23 PM3/23/09
to


Dunno. What is "callback" and "getFileFilters()?"

... but have you tried:

for (DefaultFileFilter filter : filters ) {
openDialog.addChoosableFileFilter(filter);
}
openDialog.setFileFilter( filters.get(0) );

Roedy Green

unread,
Mar 24, 2009, 2:55:15 AM3/24/09
to
On Mon, 23 Mar 2009 19:31:43 -0700 (PDT), Jason Cavett
<jason....@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>List<DefaultFileFilter> filters = new ArrayList<DefaultFileFilter>();
>filters.add(new DefaultFileFilter(".ff1", "File Filter 1"));
>filters.add(new DefaultFileFilter(".ff2", "File Filter 2"));
>filters.add(new DefaultFileFilter(".ff3", "File Filter 3"));
>
>JFileChooser openDialog = new JFileChooser();
>for (DefaultFileFilter filter : callback.getFileFilters()) {
> openDialog.setFileFilter(filter);
>}


I see several problems.

callback is undefined. Did you mean "filters"?

You want to ADD not SET the filter.

openDialog is a confusing name for a JFileChooser. Traditionally you
would use jFileChooser or jfc.

jfc.addChoosableFileFilter( new JpgFileFilter() );

It is easier to create a static array than a static ArrayList.

FileFilter[] filters = {
new DefaultFileFilter(".ff1", "File Filter 1"),
new DefaultFileFilter(".ff2", "File Filter 2"),
new DefaultFileFilter(".ff2", "File Filter 2")};


--
Roedy Green Canadian Mind Products
http://mindprod.com

"Nature provides a free lunch, but only if we control our appetites."
~ William Ruckelshaus, America’s first head of the EPA

Jason Cavett

unread,
Mar 24, 2009, 11:07:52 AM3/24/09
to
On Mar 24, 2:55 am, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:

> On Mon, 23 Mar 2009 19:31:43 -0700 (PDT), Jason Cavett
> <jason.cav...@gmail.com> wrote, quoted or indirectly quoted someone

> who said :
>
> >List<DefaultFileFilter> filters = new ArrayList<DefaultFileFilter>();
> >filters.add(new DefaultFileFilter(".ff1", "File Filter 1"));
> >filters.add(new DefaultFileFilter(".ff2", "File Filter 2"));
> >filters.add(new DefaultFileFilter(".ff3", "File Filter 3"));
>
> >JFileChooser openDialog = new JFileChooser();
> >for (DefaultFileFilter filter : callback.getFileFilters()) {
> >  openDialog.setFileFilter(filter);
> >}
>
> I see several problems.
>
> callback is undefined.  Did you mean "filters"?
>
> You want to ADD not SET the filter.  
>
> openDialog is a confusing name for a JFileChooser. Traditionally you
> would use jFileChooser or jfc.
>
> jfc.addChoosableFileFilter( new JpgFileFilter() );
>
> It is easier to create a static array than a static ArrayList.
>
> FileFilter[] filters = {
> new DefaultFileFilter(".ff1", "File Filter 1"),
> new DefaultFileFilter(".ff2", "File Filter 2"),
> new DefaultFileFilter(".ff2", "File Filter 2")};
>
> --
> Roedy Green Canadian Mind Productshttp://mindprod.com

>
> "Nature provides a free lunch, but only if we control our appetites."
> ~ William Ruckelshaus, America’s first head of the EPA

My bad - I copied this code from elsewhere. The callback
is...well...a callback that allows a developer to use my framework to
define certain "stuff" that is specific to their application. For
this example, I did mean filters.

I called it "openDialog" because it's an open dialog (opening a file
in the application).

I switched to addChoosableFileFilter, and the same problem still
occurs. (The last item added is the one that is selected.) However,
the last item is still selected. And, when I go to setFileFilter to
the proper filter (like Mark suggested), it's just added to the bottom
of the list. So, I get something like this in my filters list:

File Filter 1
File Filter 2
File Filter 3
File Filter 1 <- SELECTED

It really doesn't make sense.

Mark Space

unread,
Mar 24, 2009, 12:21:04 PM3/24/09
to
Jason Cavett wrote:

> the proper filter (like Mark suggested), it's just added to the bottom
> of the list. So, I get something like this in my filters list:
>
> File Filter 1
> File Filter 2
> File Filter 3
> File Filter 1 <- SELECTED
>
> It really doesn't make sense.


I can't tell what you are doing with out an SSCCE, but that's not what I
get. This code does what you described you wanted in your OP. I did
notice that none of these methods are marked as thread-safe when I wrote
out this example, so I called them from the EDT. Perhaps that could be
the issue?

package fubar;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;


public class JOpenFilter
{
public static void main( String... args )
{
javax.swing.SwingUtilities.invokeLater( new Runnable() {
@Override
public void run()
{
createAndShowGui();
}
} );
}

private static void createAndShowGui() {
//Create a file chooser
final JFileChooser fc = new JFileChooser();
FileNameExtensionFilter f;
fc.addChoosableFileFilter( f = new FileNameExtensionFilter(
"Fu 1", "ff1", "ffx" ) );
fc.addChoosableFileFilter( new FileNameExtensionFilter(
"Fu 2", "ff2" ) );
fc.addChoosableFileFilter( new FileNameExtensionFilter(
"Fu 3", "ff3" ) );
fc.setFileFilter( f );
int returnVal = fc.showOpenDialog( null );
System.out.println( "returned value = " + returnVal );
}
}

Jason Cavett

unread,
Mar 24, 2009, 1:56:59 PM3/24/09
to

Well, you were right about one thing - I wasn't doing the setup on the
EDT. However, when I forced that to happen (using
SwingUtilities.invokeLater). But, this didn't change what was
happening. (I did write a quick example just to make sure I was doing
it right.)

I did write an SSCCE because, well, given how large this application
is, it's impossible for me to recreate the exact problem.

Mark Space

unread,
Mar 24, 2009, 2:06:31 PM3/24/09
to
Jason Cavett wrote:

>
> Well, you were right about one thing - I wasn't doing the setup on the
> EDT. However, when I forced that to happen (using
> SwingUtilities.invokeLater). But, this didn't change what was
> happening. (I did write a quick example just to make sure I was doing
> it right.)


If your app is large and you aren't properly isolating Swing calls to
the EDT, then all bets are off. The app can do anything at all,
including things that the JLS claims are illegal for any JVM, or even
downright impossible.


>
> I did write an SSCCE because, well, given how large this application
> is, it's impossible for me to recreate the exact problem.


So JFileChooser works, the problem is in the code you aren't able to
show us.

Jason Cavett

unread,
Mar 24, 2009, 3:12:04 PM3/24/09
to

Yeah, I know JFileChooser works. Never said it didn't. In fact, I
just discovered the problem - noob mistake, actually. I looked at the
setFileFilter code, and, if the filter that is passed in is not
already in the filters list, it is added as a new filter. Well, I
*was* passing in a new reference (and didn't realize it until just
now). So, that's why the last item was always a "repeat." I
implemented equals in DefaultFileFilter and the problem is solved.

As for Swing and threading issues, it won't be a problem unless the
Swing component is already displayed and you try to change it (which
isn't what I was doing). That's why SwingUtils didn't fix the
problem.

Either way, problem is fixed. Stupid noob mistakes. Thanks for your
help.

Mark Space

unread,
Mar 24, 2009, 3:33:54 PM3/24/09
to
Jason Cavett wrote:

> As for Swing and threading issues, it won't be a problem unless the
> Swing component is already displayed and you try to change it (which
> isn't what I was doing). That's why SwingUtils didn't fix the
> problem.


This is completely untrue. Who says that threading issue don't matter
if components aren't displayed? That's ridiculous.

0 new messages