Adding FileFilters to JFileChooser

J

Jason Cavett

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?
 
M

Mark Space

Jason said:
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?


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

.... but have you tried:

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

Roedy Green

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
 
J

Jason Cavett

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.
 
M

Mark Space

Jason said:
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 );
}
}
 
J

Jason Cavett

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

}

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.
 
M

Mark Space

Jason said:
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.
 
J

Jason Cavett

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.




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

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.
 
M

Mark Space

Jason said:
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.
 

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,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top