Article View: borland.public.jbuilder.java.swing
Article #138Re: JFileChooser Filters
From: John Hetherington john.hethering...
Date: Mon, 27 Feb 2006 23:19
Date: Mon, 27 Feb 2006 23:19
97 lines
3930 bytes
3930 bytes
Arthur
Many thanks for your help, I created a myFilter class which had its Base as javax.swing.filechooser.FileFilter and popped your code in. No problems now it works fine. I shall read the sun site material you suggested. All this gives a point to explore with more confidence.
Much appreciated
John in NZ
"Arthur Ore" <nospamthx@nospamthx.co.uk> wrote:
>Hi John,
>
>For a full explanation of JFileChooser have a look at Sun's Swing Tutorial
>:-
>
>http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html
>
>Below is some code which may do what you want. Personally I normally try to
>avoid inner classes, but I knocked up this code to show you that what you
>want to achieve can be done fairly simply. Most of the code is needed as
>Java doesn't provide a convenience method to access a file's extension.
>
>If you look at the bottom of Sun's tutorial you'll see some examples;
>FileChooserDemo2 gives a more complete example
>
>JFileChooser jFileChooser1 = new JFileChooser();
>jFileChooser1.setAcceptAllFileFilterUsed(false);
>jFileChooser1.addChoosableFileFilter(
> new FileFilter()
> {
> public boolean accept(File f)
> {
> if (f.isDirectory())
> {
> return true;
> }
> else
> {
> String fileExtn = null;
> String fname = f.getName();
> int i = fname.lastIndexOf('.');
>
> if (i > 0)
> {
> fileExtn = fname.substring(i + 1).toLowerCase();
> if (fileExtn.equals("jpg") || fileExtn.equals("gif") ||
>fileExtn.equals("odt"))
> {
> return true;
> }
> else
> {
> return false;
> }
>
> }
> else
> {
> return false;
> }
> }
> }
>
> public String getDescription()
> {
> return "Images etc";
> }
>
> }
> );
>
>Arth
>
>"John Hetherington" <janetandjohn@clear.net.nz> wrote in message
>news:4400d65c$1@newsgroups.borland.com...
>>
>> Hello from New Zealand,
>>
>> Can anyone help with setting up a simple multiple filter on the Swing
>> FileChooser component, I have used C++ Builder before and it was a matter
>> of using a mask editor, 2 minutes tops.
>> I can't get to grips with the same move on JBuilder. Suppose I want to
>> have my File Open dialog display a filter list for the *.odt files , *.jpg
>> and *.gif files. The sun site says I have to create a Filter class, I
>> notice one of the properties of the JFilechooser component is a filter?.
>>
>> Can anyone give me a leg up on this.
>>
>>
>> Kindest Regards
>>
>> John Hetherington
>
>
Technical Details
Message-ID:
<4403970f$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news4.google.com!news2.volia.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<4400d65c$1@newsgroups.borland.com> <4400f151$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 111d45,399240278cac5c83 X-Google-Attributes: gid111d45,public X-Google-Language: ENGLISH,ASCII-7-bit From: "John Hetherington" john.hetherington Sender: "John Hetherington" john.hetherington Reply-To: "John Hetherington" john.hetherington Subject: Re: JFileChooser Filters Newsgroups: borland.public.jbuilder.java.swing X-User-Info: 203.167.214.18 X-Forwarded-For: 127.0.0.1 References: <4400d65c$1@newsgroups.borland.com> <4400f151$1@newsgroups.borland.com> NNTP-Posting-Host: 207.105.83.66 Message-ID: <4403970f$1@newsgroups.borland.com> Date: 27 Feb 2006 16:19:27 -0700 X-Trace: newsgroups.borland.com 1141085967 207.105.83.66 (27 Feb 2006 16:19:27 -0700) Lines: 98 Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news4.google.com!news2.volia.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail