Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Darren - 26 Oct 2005 01:20 GMT
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";
   }
}
Roedy Green - 26 Oct 2005 03:28 GMT
>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.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Darren - 26 Oct 2005 15:34 GMT
> >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

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
[quoted text clipped - 3 lines]
> 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
[quoted text clipped - 3 lines]
> 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? :)

> Now lets get to the meat of your problem.
>
[quoted text clipped - 8 lines]
>
> Is that enough of a hint?  If not, scroll down.

sorted.

> There are TWO different FileFilter interfaces:
>
[quoted text clipped - 9 lines]
> I documented it at http://mindprod.com/jgloss/jfilechooser.html
> long ago.

It gets us all in the end.
Roedy Green - 27 Oct 2005 00:44 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Darren - 27 Oct 2005 01:17 GMT
> >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.

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.
Roedy Green - 27 Oct 2005 00:46 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Darren - 27 Oct 2005 01:12 GMT
> >however my naming convention is similar but not identical to
> >yours however I won't bore you with the details.
[quoted text clipped - 3 lines]
> are standard, and they are almost universally observed by professional
> programmers.

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

> I am not talking about how may spaces you indent.
Neither am I.
Roedy Green - 27 Oct 2005 02:00 GMT
On Wed, 26 Oct 2005 23:46:36 GMT, Roedy Green
<my_email_is_posted_on_my_website@munged.invalid> wrote, quoted or
indirectly quoted someone who said :

>>however my naming convention is similar but not identical to
>>yours however I won't bore you with the details.
[quoted text clipped - 3 lines]
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Darren - 27 Oct 2005 09:37 GMT
> On Wed, 26 Oct 2005 23:46:36 GMT, Roedy Green
> <my_email_is_posted_on_my_website@munged.invalid> wrote, quoted or
[quoted text clipped - 10 lines]
> 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
[quoted text clipped - 3 lines]
> 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.
>
[quoted text clipped - 5 lines]
> 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.
Roedy Green - 27 Oct 2005 00:49 GMT
>> 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? :)

It won't happen again.

See http://mindprod.com/jgloss/newsgroups.html#GIFTHORSE
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Darren - 27 Oct 2005 01:14 GMT
> >> 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
[quoted text clipped - 5 lines]
>
> See http://mindprod.com/jgloss/newsgroups.html#GIFTHORSE

So i'm sure you'll point out where i was ungracious and i'll be sure to
point out where you was patrozising.
Darren - 27 Oct 2005 01:58 GMT
> >> 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
[quoted text clipped - 5 lines]
>
> See http://mindprod.com/jgloss/newsgroups.html#GIFTHORSE

http://homepage.ntlworld.com/darrenls59/cliches.htm


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.