🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.jbuilder.java.swing
3 messages
3 total messages Started by John Hetherington Sat, 25 Feb 2006 21:12
JFileChooser Filters
#136
Author: John Hetherington
Date: Sat, 25 Feb 2006 21:12
12 lines
1400 bytes
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
Re: JFileChooser Filters
#137
Author: Arthur Ore
Date: Sun, 26 Feb 2006 00:07
82 lines
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 
Re: JFileChooser Filters
#138
Author: John Hetherington john.hethering...
Date: Mon, 27 Feb 2006 23:19
97 lines
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 
>
>
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads