Article View: borland.public.jbuilder.java.swing
Article #137Re: JFileChooser Filters
From: Arthur Ore
Date: Sun, 26 Feb 2006 00:07
Date: Sun, 26 Feb 2006 00:07
82 lines
3528 bytes
3528 bytes
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:
<4400f151$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<4400d65c$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: "Arthur Ore" <nospamthx@nospamthx.co.uk> Newsgroups: borland.public.jbuilder.java.swing References: <4400d65c$1@newsgroups.borland.com> Subject: Re: JFileChooser Filters Date: Sun, 26 Feb 2006 00:07:43 -0000 Lines: 84 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2670 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670 X-RFC2646: Format=Flowed; Original NNTP-Posting-Host: 82.69.65.131 Message-ID: <4400f151$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1140912465 82.69.65.131 (25 Feb 2006 16:07:45 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail