setting file filter to JFileChooser

hi iam trying to add a file filer to a JFileChooser...

i have made FileFilter implements FileFilter ( below ) , the code wont run
what am i doing wrong here ??

      

String filename = "timmy"+File.separator+"tmp"; 
	JFileChooser fc = new JFileChooser(new File(filename));
	
fc.setFileView(new FileFilterImp());
fc.setFileFilter(new FileFilterImp());



--------------------------------------------------

import java.io.File;
import java.io.FileFilter;

public class FileFilterImp implements FileFilter
	
{
	  private final String[] okFileExtensions =  new String[] {"txt", "csv", "gif"};

	  
	  // need to write the accept method
	  
	  public boolean accept(File file)
	  {
	    for (String extension : okFileExtensions)
	    {
	      if (file.getName().toLowerCase().endsWith(extension))
	      {
	        return true;
	      }
	    }
	    return false;
	  }
	
	

}

Open in new window

SYMONAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
That's not going to work. If say, it's csv, the method will return false. It will only return true for .txt, since you use a loop. You could fix the loop, but it will be more efficient to use a HashSet OK_FILE_EXTENSIONS (constant, final) and use OK_FILE_EXTENSIONS.contains(x)
0
objectsCommented:
you're almost there, should be more like this:

class FileFilterImp extends FileFilter {
      private final String[] okFileExtensions = new String[] { "txt", "csv", "gif" };

      // need to write the accept method
      public boolean accept(File file) {
            for (String extension : okFileExtensions) {
                  if (file.getName().toLowerCase().endsWith(extension)) {
                        return true;
                  }
            }
            return false;
      }

      @Override
      public String getDescription() {
            return "My files";
      }

}

> fc.setFileView(new FileFilterImp());

you'll need a seperate class for the FileView

fc.setFileView(new FileViewImp());
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
objectsCommented:
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

SYMONAuthor Commented:
i have tried adding the getDesciption() method to the class FileFilterImp extends FileFilter...

but there is still a problem eclipse keeps warning ..

- The method setFileView(FileView) in the type JFileChooser is not applicable for the arguments
       (FileFilterImp)
      - The method setFileFilter(FileFilter) in the type JFileChooser is not applicable for the arguments
       (FileFilterImp)

but i though if a class extends a class it is of that type ?
0
CEHJCommented:
I's not of type FileView is it?
0
objectsCommented:
> - The method setFileView(FileView) in the type JFileChooser is not applicable for the arguments

you need a seperate class for that, you can't have one class do both

> but i though if a class extends a class it is of that type ?

did you change it to extend as I suggested above?
you'll also need to fix your import
0
CEHJCommented:
... and it should be 'implements FileFilter'
0
SYMONAuthor Commented:
also

OK_FILE_EXTENSIONS (constant, final) and use OK_FILE_EXTENSIONS.contains(x)...

i understand hash's but what is the constant and final ...
final = File.extensions.something
const = number reprenting attribute >?
0
CEHJCommented:
It doesn't 'extend' anything. FileFilter is an interface. Please post your current code
0
objectsCommented:
> OK_FILE_EXTENSIONS (constant, final) and use OK_FILE_EXTENSIONS.contains(x)...

you don't need that, the code you have is fine
see the class I posted earlier
0
CEHJCommented:
>>i understand hash's but what is the constant and final ...

I mentioned that since you can declare the HashSet as a constant


private final Set OK_FILE_EXTENSIONS = new HashSet(.............)

Open in new window

0
CEHJCommented:
Although you should probably genericise it:


private final Set<String> OK_FILE_EXTENSIONS = new HashSet<String>(.............)

Open in new window

0
SYMONAuthor Commented:
... and it should be 'implements FileFilter'

yeah i can't extend it beacuse FileFilter can't be instantiated ? because its abstract ?? ..

so i have implented the FileFilter interface in FileFilterImp...  

will attemp view imp...
0
objectsCommented:
> ... and it should be 'implements FileFilter'

no, it needs to extend as I already explained.
see the code I already posted above, it works fine :)
0
CEHJCommented:
>>will attemp view imp...

Why do you want to do that btw?
0
CEHJCommented:
If you're using javax.swing.filechooser.FileFilter, it should be extends, not implements
0
objectsCommented:
CEHJ> ... and it should be 'implements FileFilter'

CEHJ>  it should be extends, not implements

guess it helps to hedge your bets :-D
wastes peoples time though
0
SYMONAuthor Commented:
thanks implemented a fileViewImpl which extends FileView...

still not doing what i want though .. will view more examples
0
CEHJCommented:
SYMON, can you tell me why you accepted that answer? The issue of extends/implements was nothing to do with your problem - as is proved by the fact you had the correct keyword in the first place
0
SYMONAuthor Commented:
hi CEHJ,

first of all i am kind new to this and i think that sometimes i articulate my questions poorly as i am in a "context" acquisition phase of learning

what i was trying to do, was load a file filter that i contructed to show only .csv and text files when i opened the JFileChooser, what i would also have liked would be if the list of file extensions that i wanted to save the files as were displayed as options in the file combo box inside the jchooser...

why i awarded the points, because i guess that solution helped the most, i probably should have awarded some points for the hashmap, which i always prefered for unique entries, but it was the extends, implements a class difference that at least allowed me to implement my own file filter, which can be used in many other applications.. i have written my own version but it sure looks handy...

sorry if this caused problems
0
objectsCommented:
your question was articulated just fine :)

>  i probably should have awarded some points for the hashmap

not really as it was unrelated to your problem
0
CEHJCommented:
>>why i awarded the points, because i guess that solution helped the most,

It wasn't a solution - just a rude comment. Changing the keyword won't solve your problem - it's only part of a solution, since the more serious problem with what you had, i outlined at http:#29317484
0
objectsCommented:
based on what exactly?
A Map was in no way required as shown by the solution provided by me?

CEHJ claims that it was only part of the solution are incorrect.
0
objectsCommented:
and now you have an accepted solution that does *not* solve the problem. How is that helpful?
0
objectsCommented:
>  Changing the keyword won't solve your problem

It obviously does solve the problem (otherwise why would it be accepted).
Did anyone actually bother to test the code I posted??

> since the more serious problem with what you had, i outlined at http:#29317484

That comment was wrong, as shown by the fact that what I posted worked and was accepted
0
objectsCommented:
The following comments by CEHJ have also been shown to be incorrect, though you still choose to accept his comment. Sorry but this just doesn't make sense

> That's not going to work. If say, it's csv, the method will return false. It will only return true for .txt, since you use a loop.

> ... and it should be 'implements FileFilter'

>  The issue of extends/implements was nothing to do with your problem - as is proved by the fact you had the correct keyword in the first place
0
SYMONAuthor Commented:
Dear moderator ?

how do i fix this ? should i be splitting points ? i thought that this was already done by another moderator..?

i appreciated every ones contribution but clearly people aren't happy ?

what can be done to make this right ?
0
SYMONAuthor Commented:
I am totally up for splitting the points !!! and i really appreciate the help and both solutions were valuable to me :)

and yes i understand both solutions and have adopted both, very happy to split to resolve issue

:)
0
objectsCommented:
> for comment 29320301(yes, it is a wrong type)
CEHJ> I's not of type FileView is it?

repeats what I had already stated
objects> you'll need a seperate class for the FileView

> or 29320814 (that's probably the most reasonable question in that mess - why does he need a new class at all)
SYMON>>will attemp view imp...
CEHJ>Why do you want to do that btw?

Again I had already explained that a seperate class was needed for FileView (SYMON was responding to me in his comment)

> or 29320940 (finally someone decided to tell the asker why he needs to extend now and what class he actually needs to extend).

CEHJ> If you're using javax.swing.filechooser.FileFilter, it should be extends, not implements

I had already shown that, though CEHJ fails to mention that (basic professional courtesy).
And CEHJ also states:

CEHJ> ... and it should be 'implements FileFilter'
CEHJ> The issue of extends/implements was nothing to do with your problem - as is proved by the fact you had the correct keyword in the first place
CEHJ> Changing the keyword won't solve your problem

All his cross posting made it very hard to actually explain anything


CEHJ demanded this be reopened because he believed that what I posted was wrong. Now that that has been proven incorrect you appear to be hunting for other reasons to split. Which is fine (it not like I need the points) though I just don't understand why. If you want to go digging for comments relevant to the solution then you could reopen the majority of questions on EE. I have many relevant comments ignored every day.

Personally I'd think the question is worth deleting as the amount of misinformation posted makes it little value as a PAQ.

And next time you want to reopen a question perhaps contact me first to avoid wasting lots of peoples time :)
0
objectsCommented:
>  but then they lapsed in a strange behaviour

sorry but what exactly was my *strange behaviour*?
Would love to rectify it for you

> but then they lapsed in a strange behaviour and remarks that caused a list of Moderators and Zone Advisors to get involved...

next time just contact me instead of accepting an incorrect comment and stirring up a hornets nest. far more effective
0
objectsCommented:
>  instead of helping the asker you started arguing over what the other said... Again.

sorry but I don't see where I was arguing with anyone, please be specific about where

> Because I can read Java code worse than you do? :)

No because I was more familiar with the question, and we could have avoided this mess

> And if you had explained calmly why exactly the initial code works

sorry but I didn't see a need, I was focussing on answering the question. ie. the problem with the existing code.
I don't have the time to correct every incorrect post made on EE. And history shows anyone correcting CEHJ generally just gets flamed.
0
VenabiliCommented:
But you posted enough times that he had been wrong... without explaining why the code really works. It was obvious that both the Asker and CEHJ thought that need correcting, explaining why not  would have helped.

I can understand you not wanting to explain why it works but can we agree that if you had posted that you use javax.swing class now instead of the Interface in the very first comment, we would not be here now? Instead of helping another asker...

>And history shows anyone correcting CEHJ generally just gets flamed.
Let's not get there, can we? Both of you take very badly any correction when you still believe you are right - and for some reason when you two are doing it, it is never with an explantion why - so things get worse.

PS: You realise that asking you here would have been  a bit strange when you are one side of the dispute, right?

>sorry but I didn't see a need, I was focussing on answering the question. ie. the problem with the existing code.
OK. And where did you explain why implements became extends (which was the main problem once your code was posted?). Repeating "try it" was not heling because obviously the asker did not understand the change - even if it was logical and clear if you know Java enough.  We can go in circles as much as we want here - that won't make the whole situation less messy (even before the Mods got involved).

And looking at the times - in a lot of cases one lof you was probably posting before seeing the other one's comment - which added to the confusion, missunderstanding and the whole state of the simple question that started it all...

So anything against accepting your code with assist from CEHJ (the comment where he names the FileFilter class that you use - and which finishes the solution and got the asker less confused)? 400/100 for example?
0
objectsCommented:
you didn't answer my question :)  where exactly was I being argumentative?

> PS: You realise that asking you here would have been  a bit strange when you are one side of the dispute, right?

what dispute exactly. clarification was asked for and it was given by SYMON. I couldn't see why this didn't all stop there
though what you ended up doing any *was* ask me :)

> Both of you take very badly any correction when you still believe you are right

sorry disagree, please support those sort of statements with some evidence
0
objectsCommented:
sorry I really don't want to get into this but its just annoying when I get accused of being argumentative when I honestly don't feel I have been in any way.
0
VenabiliCommented:
PS:
>no, it needs to extend as I already explained.
You never explained really, you just showed. Which, I will say again, is the basic issue all along -- the asker really needed the extra help.

Then how do you call the "hedging the bets" comment when you got two comments posted for different things and put them together?:) And continiously repeating someone to test your code because something else is not needed but not explaining why it is not needed is almost argumentative... even when your code is solving it...

OK - I wil start keeping notes when I see any of you getting unhappy for someone correcting you - and here I was believing that you had stopped for a long while... :) Just do not play the innocent that always get caught by the bad experts... :)

Let's give CEHJ a chance to review all this here and we will see where we go from there - again, his initial comment is not correct so let's see what he thinks on the rest. And we can delete all these offtopic discussion at the end...

0
VenabiliCommented:
No worries, objects

If this whole thing will help getting you and CEHJ either working together or ignoring each other (which will be a pity), we can get through it. But you spent so much time saying how the changes are not needed that you could have explained why instead of just saying.... :)
0
objectsCommented:
> >no, it needs to extend as I already explained.

that was responding to SYMON's comment, had nothing to do with CEHJ

> Then how do you call the "hedging the bets" comment when you got two comments posted for different things and put them together?:)

I'll cop that one, but it was getting a little frustrating at that point as it was confusing SYMON

One comment hardly makes me argumentative :)  



0
VenabiliCommented:
I have a proposal - I will open a hidden thread - you and me (And one for me and CEHJ) so when you feel like he gets a bit over the top or vice versa, you can contact me? Or you can use my mail? Instead of posting this type of comments? I know why you posted it but you realise that it just inflamed the already flameable situation, right? :)

Let's try to work all this out? Because when I see an objection from one of you, I always worry that it will be something like this here. And we had been at the same way too often - one of you do something, the other fails for it and here we are. And the problem is that either of you can claim that they behaved properly. With the timezones, the communication between the two of you will always be a mess... so let's see what we can do.

Do you want to try this for a while?

CEHJ,
Same applies to you. You nterested in trying to get you two to stop yanking each other's chains?
0
VenabiliCommented:
> that was responding to SYMON's comment, had nothing to do with CEHJ

I know that. But it was not heping SYMON either and at this point you had the chance to say why it needs to extent now and all would have been done...
0
objectsCommented:
> But it was not heping SYMON either and at this point you had the chance to say why it needs to extent now and all would have been done...

still doesn't make it argumentative. How I answer questions is up to me and askers seem pretty happy with how I operate.

> Do you want to try this for a while?

no thanks sorry. been there, done that and was proven to be a complete waste of time (on more than one occasion)
0
CEHJCommented:
Sorry - been held up with Easter festivities.

Yes, i was getting confused on certain issues - i'll admit that. My first comment is partly wrong and partly right. The current iteration is O(n), whereas the HashSet would be near O(1), which is more efficient, as i mentioned
0
SYMONAuthor Commented:
Dear CEHJ and objects,

You are BOTH LEGENDS !!!!

I understand complexity ( and generally mathematics much better than code :)) and i don't know why i wasn't using a hash, i use them all the time in perl, and in the accounting parts of stuff i made in java .... BUT it was TOTALLY GREAT to be reminded :) seriously.. i actually went through and replaced a bunch of libraries that i have written with hash's = extremely valuable reminder on BEST code practice...

I actually come from "alot of crazy ass math and stat modeling that you'll never use" background, i have been coding now for about 4 years, i made my own business and make "real time info systems" and other stuff...

I don't really have a problem working stuff out, but am trying to get outa the whole re-inventing the wheel phenomena, and actually just make the best code i can that is readable in 6 months time..

To be honest i find working with existing models more difficult, and i think that has alot to do with my
ignorance of context, plus just gaps in the story, where i have pieces of the java puzzle worked out but
struggle to put them together... or just confuse myself :)

hence i needed help to realise that i needed to extend the class :) so that it could be passed in as an
argument, again GOLD !!! , ( see confusing myself ) it set off a cascade of reactions in my code, i actually looked at that piece of code about 2 years ago couldn't work it out and so just left it...

from my side of things i am only really interested in one thing, mastering the art of code :)
 i realise that this is probably going to take 150 years.. but hey, i really like making stuff :)

the specific analysis of the portion and measure that the solutions map to the question is unambiguously secondary to making the best code that i can.

to which end both of your solutions were insightful , helpful and improved my code.

I appreciate your insight and  thankyou :):):)




0
CEHJCommented:
Thanks SYMON
0
quomodoCommented:
All,

Did we come to a resolution regarding which comment/s to accept as the solution?

quomodo
Community Support Moderator
0
objectsCommented:
I believe so:

So anything against accepting your code with assist from CEHJ (the comment where he names the FileFilter class that you use - and which finishes the solution and got the asker less confused)? 400/100 for example?
0
CEHJCommented:
Well, to reiterate, it was mainly a blunder on my part, apart from the recommendation to avoid O(n) searching. If that's worthy of credit, so be it.
0
VenabiliCommented:
Accept http:#a29318693
Assist http:#a29320940 
400/100
0
SYMONAuthor Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.