addChoosableFileFilter in javax.swing.JFileChooser cannot be applied to zipfilter error

D

Darren

When i call the below i get the abover error message
fc.addChoosableFileFilter(new zipfilter());
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = fc.showOpenDialog(frame);

as i see it the parameter should be a class inhereted from FileFilter. This
is what i did. I copied the example from Sun's own site and modified but i
get errors when i try to use it as a filter. zipfilter is declared below.

Why am I getting errors?

Thanks
// zipfile.java

import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.*;

/* ImageFilter.java is a 1.4 example used by FileChooserDemo2.java. */
public class zipfilter extends FileFilter {

//Accept all directories and all gif, jpg, tiff, or png files.
public boolean accept(File f)
{
if (f.isDirectory())
{
return true;
}

String extension = Utils.getExtension(f);
if (extension != null) {
if (extension.equals(Utils.zip) ||
extension.equals(Utils.jar))
{
return true;
}
else
{
return false;
}
}

return false;
}

//The description of this filter
public String getDescription()
{
return "Archives";
}
}
 
R

Roedy Green

fc.addChoosableFileFilter(new zipfilter());

A few thoughts peripheral to your actual question before I answer it.

0. The answer was waiting for you all along at
http://mindprod.com/jgloss/jfilechooser.html

1. When you want others to help, post COMPLETE code.
Your problem, for example, potentially could be in the declaration of
fc. People have to guess what class fc is and which class
addChooseableFileFilter belongs too. Since people don't like doing
that extra work, fewer people will bother with your problem.
I know it is obvious to you it is a JFileChooser, but to your readers,
it may be something else or you may have screwed up the declaration.

2. A class should begin with a capital letter, namely ZipFilter. When
you ignore naming conventions it confuses the people
trying to help you and it is also disrespectful of them; it makes it
look as if there are errors where there are not; it makes it look as
if you are confused between classes, methods and variables where you
may not be, eliciting some unwanted explanation. see
http://mindprod.com/jgloss/codingconventions.html

3. ZipFilter is an ambiguous name. It suggest it is something for
filtering a list of zip files or the contents of zip files. I would
call it something like "ZipsOnlyFilter" or "DotZipOnlyFilter" or
"ZipExtensionOnlyFilter" or best: "ChooseOnlyZipsFilter" to make it a
little clearer.

4. repeat details in your title line in the body. The title is just
to grab attention. It is not logically part of the body and can be
easily overlooked in trying to decipher your post.

Now lets get to the meat of your problem.

The javadoc says:

void JFileChooser.addChoosableFileFilter(FileFilter filter)
Adds a filter to the list of user choosable file filters.

What is a FileFilter? Click the JavaDoc and you will see:

javax.swing.filechooser.FileFilter

Is that enough of a hint? If not, scroll down.




























There are TWO different FileFilter interfaces:

java.io.FileFilter
and
javax.swing.filechooser.FileFilter

You were not specific. This is one reason to avoid the import *
notation.
see http://mindprod.com/jgloss/import.html

I got nailed on the exact same thing that's how I know, and that's why
I documented it at http://mindprod.com/jgloss/jfilechooser.html
long ago.
 
D

Darren

Roedy Green said:
A few thoughts peripheral to your actual question before I answer it.

0. The answer was waiting for you all along at
http://mindprod.com/jgloss/jfilechooser.html

Yep so it is.I forgot to look there. :)
1. When you want others to help, post COMPLETE code.
Your problem, for example, potentially could be in the declaration of
fc. People have to guess what class fc is and which class
addChooseableFileFilter belongs too. Since people don't like doing
that extra work, fewer people will bother with your problem.
I know it is obvious to you it is a JFileChooser, but to your readers,
it may be something else or you may have screwed up the declaration.
my classes are in multiple files but I'll isolate what I think covers
everything needed in future..
2. A class should begin with a capital letter, namely ZipFilter. When
you ignore naming conventions it confuses the people
trying to help you and it is also disrespectful of them; it makes it
look as if there are errors where there are not; it makes it look as
if you are confused between classes, methods and variables where you
may not be, eliciting some unwanted explanation. see
http://mindprod.com/jgloss/codingconventions.html

In my many eons. since I started bashing and fubling together programs I
have come across a lot of differnt coding conventions, some depending on the
language, some depending on the environment and some depending which company
i was working for ow with all thiese fudging around in my burnt out brain i
kindof lose track of who needs what protocol so I tend to use the one I'm
most familiar with however this time i broke my own conventions so smacky
wrists for me. however my naming convention is similar but not identical to
yours however I won't bore you with the details.

However if your not adversed to a bit of constructive criticism, in your
example "File file = fc.getSelectedFile();" sucks and it breaks to of my
conventions :p
3. ZipFilter is an ambiguous name. It suggest it is something for
filtering a list of zip files or the contents of zip files. I would
call it something like "ZipsOnlyFilter" or "DotZipOnlyFilter" or
"ZipExtensionOnlyFilter" or best: "ChooseOnlyZipsFilter" to make it a
little clearer.

I chose ZipFileFilter. That does me :)
4. repeat details in your title line in the body. The title is just
to grab attention. It is not logically part of the body and can be
easily overlooked in trying to decipher your post.

I got your attention didn't I? :)
 
R

Roedy Green

my classes are in multiple files but I'll isolate what I think covers
everything needed in future..

You want complete code so others can compile and run it. We don't
always find the problems just by looking at the code.
 
R

Roedy Green

however my naming convention is similar but not identical to
yours however I won't bore you with the details.

Coding conventions in Java are not like coding conventions in other
languages. The basic naming rules are taken much more seriously. They
are standard, and they are almost universally observed by professional
programmers.

I am not talking about how may spaces you indent.
 
D

Darren

Roedy Green said:
Coding conventions in Java are not like coding conventions in other
languages. The basic naming rules are taken much more seriously. They
are standard, and they are almost universally observed by professional
programmers.

Well i doubt you'll have much trouble with mine.
 
D

Darren

Roedy Green said:
You want complete code so others can compile and run it. We don't
always find the problems just by looking at the code.

Well if people want the whole thing they only have to ask. Some people don't
like mutiple files full of code on a newsgroup posted when the user wants a
single problem fixing.
 
R

Roedy Green

Coding conventions in Java are not like coding conventions in other
languages. The basic naming rules are taken much more seriously. They
are standard, and they are almost universally observed by professional
programmers.

The mental model I have for answering posts that I am back at
university teaching a tutorial in elementary computer science.

I did much of my teaching by encouraging students to ask questions. I
found that helped keep the students engaged. So whenever I answered a
question, my main concern was using the answer to advance the
knowledge of the class as a whole, not just to see that one particular
student got the point.

This view of course clashes with questioners who see me as sort of
like the guy at the post office who is there to sell them stamps, not
tell them how to run their lives.

What's in for me to answer posts?

1. it furthers my long term goal of educating the third world, my
lifelong dream. There is so much you learn just from varied
programming experience since 1963, that nobody writes down, or even
clearly formulates in their own heads. It is experienced as a gut
feel on what will work best. I try to make that explicit through
metaphors, rules of thumb, polishing examples, etc. I try to use
colourful language that will stick in the imagination.

2. I broaden my knowledge by poking into all manner of problems I
would not directly encounter in the course of my usual work. I can
pick and choose what looks most interesting. I am a compulsive
documenter, and it gives me something to document. I have poor health
and a short expected lifespan. Legacy is high on my mind. The Java
glossary and my teaching is my contribution to the species.

3. I get some exposure that can lead to contract work.

4. I sometimes get appreciation from those who learned something or
got a problem solved. For me the expression of the appreciation is
not important. The pleasure comes from knowing the questioner and the
audience now have a deeper understanding. The thing that makes me
happiest is seeing the triumphant words "It worked!" Unfortunately,
the unpleasantness of some of the interactions far overshadows that
benefit.
 
D

Darren

Roedy Green said:
The mental model I have for answering posts that I am back at
university teaching a tutorial in elementary computer science.

No problem there and you are a good teacher but sometimes you come across
as being patronising.
I did much of my teaching by encouraging students to ask questions. I
found that helped keep the students engaged. So whenever I answered a
question, my main concern was using the answer to advance the
knowledge of the class as a whole, not just to see that one particular
student got the point.

I too often answer questions on other newsgroups, chat rooms on certain
topics but my aim is to make sure everyone understands. It's not easy but I
persist as do you.
This view of course clashes with questioners who see me as sort of
like the guy at the post office who is there to sell them stamps, not
tell them how to run their lives.

What's in for me to answer posts?

The same as what's in it for me, to get the satisfaction of teaching anyone
who wants to learn.
1. it furthers my long term goal of educating the third world, my
lifelong dream. There is so much you learn just from varied
programming experience since 1963, that nobody writes down, or even
clearly formulates in their own heads. It is experienced as a gut
feel on what will work best. I try to make that explicit through
metaphors, rules of thumb, polishing examples, etc. I try to use
colourful language that will stick in the imagination.

Good plan, it makes the lesson interesting. My IT history goes back to 1983,
not as long as yours but long enough to pick up a trick or two.
2. I broaden my knowledge by poking into all manner of problems I
would not directly encounter in the course of my usual work. I can
pick and choose what looks most interesting. I am a compulsive
documenter, and it gives me something to document. I have poor health
and a short expected lifespan. Legacy is high on my mind. The Java
glossary and my teaching is my contribution to the species.

I'm sorry to hear about your health, your glossary is very good, it
addresses topics Sun covers briefly and you do it clearly and concisely.
3. I get some exposure that can lead to contract work.

4. I sometimes get appreciation from those who learned something or
got a problem solved. For me the expression of the appreciation is
not important. The pleasure comes from knowing the questioner and the
audience now have a deeper understanding. The thing that makes me
happiest is seeing the triumphant words "It worked!" Unfortunately,
the unpleasantness of some of the interactions far overshadows that
benefit.

Well for what it's worth your halp has been and always will be appreciated.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top