473,320 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

FileFilter

SammyB
807 Expert 512MB
... Next chapter is files; finally will have some data to work with. Now, if the weather would just get warm for one more bike ride before the snow!
Well, the weather did get warm and I got in two bike rides, the file chapter was a disappointment (just a whitewash of DataOutputStream), so I decided to try DataInputStream which was also easy, BUT ...

I wanted to add a GetOpenFileName dialog box (the book just hardcodes the filename), but I am mildly flummoxed by the FileFilter. The test program below works, but there is only one choice, Data Files. How can I have two choices in the Files of Type drop-down, say Text files (*.txt) and Data files (*.dat). Also, just a nit: in the DOS world, All files is usually the last choice, not the first. Can I put All files at the bottom of the list? TIA --Sam

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import javax.swing.*;
  3. class DatFilter extends javax.swing.filechooser.FileFilter
  4. {
  5.  public boolean accept(File f)
  6.  {
  7.   return f.getName().endsWith(".dat");
  8.  }
  9.  public String getDescription () 
  10.  {
  11.      return "Data files (*.dat)";
  12.  }
  13. }
  14. public class TestChooser
  15. {
  16.  public static void main(String[] args) 
  17.  {
  18.   JFileChooser fc = new JFileChooser();
  19.   fc.setDialogTitle ("Open Payment File");
  20.   fc.setFileSelectionMode ( JFileChooser.FILES_ONLY);
  21.   fc.setCurrentDirectory (new File ("."));
  22.   fc.setFileFilter(new DatFilter());
  23.   int result = fc.showOpenDialog (null);
  24.      if (result == JFileChooser.CANCEL_OPTION) 
  25.            System.exit(0);
  26.      File fFile = fc.getSelectedFile();
  27.   JOptionPane.showMessageDialog(null, fFile.getName());
  28.  }
  29. }
  30.  
Dec 13 '07 #1
4 3073
JosAH
11,448 Expert 8TB
Did you read about the addChoosableFileFilter() method?

kind regards,

Jos
Dec 13 '07 #2
SammyB
807 Expert 512MB
Did you read about the addChoosableFileFilter() method?

kind regards,

Jos
So little time, so much to read! But, a quick look says that's what I want. Thanks! --Sam
Dec 13 '07 #3
JosAH
11,448 Expert 8TB
So little time, so much to read! But, a quick look says that's what I want. Thanks! --Sam
The 'mechanism' behind this Filter stuff is a bit strange: you can add or remove the
filters but if you 'set' one it is implicitly added to that list. You can also enable or
disble the 'accept all' filter. If you 'set' a filter that is already present in the list, it
is just made the currently active filter.

kind regards,

Jos
Dec 13 '07 #4
SammyB
807 Expert 512MB
Well, that worked just as you said, but it was a little confusing so I thought I'd post the code, so everyone can see it.
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import javax.swing.*;
  3. import javax.swing.filechooser.FileNameExtensionFilter;
  4. class AllFilter extends javax.swing.filechooser.FileFilter
  5. {
  6.    String mDescription;
  7.    AllFilter(String Description)
  8.    {
  9.       mDescription = Description;
  10.    }
  11.    public boolean accept(File f)
  12.    {
  13.       return true;
  14.    }
  15.    public String getDescription () 
  16.    {
  17.        return mDescription;
  18.    }
  19. }
  20. public class TestChooser
  21. {
  22.    public static void main(String[] args) 
  23.    {
  24.       JFileChooser fc = new JFileChooser();
  25.       fc.setDialogTitle ("Open Payment File");
  26.       fc.setFileSelectionMode ( JFileChooser.FILES_ONLY);
  27.       fc.setCurrentDirectory (new File ("."));
  28.       fc.setAcceptAllFileFilterUsed(false);      // So that it will appear at the end
  29.       fc.addChoosableFileFilter(new FileNameExtensionFilter("Text files (*.txt)","txt"));
  30.       FileNameExtensionFilter dat = new FileNameExtensionFilter("Data files (*.dat)","dat");
  31.       fc.addChoosableFileFilter(dat);
  32.       fc.addChoosableFileFilter(new AllFilter("All files (*.*)"));
  33.       fc.setFileFilter(dat);
  34.       int result = fc.showOpenDialog (null);
  35.        if (result == JFileChooser.CANCEL_OPTION) 
  36.              System.exit(0);
  37.        File fFile = fc.getSelectedFile();
  38.       JOptionPane.showMessageDialog(null, fFile.getName());
  39.    }
  40. }
  41.  
Dec 29 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Alexander DEJANOVSKI | last post by:
Hi all, I'm starting a new project to develop an Open-Source EAI server in Python and I'm looking for motivated developpers and testers. It is inspired by Open Adaptor (www.openadaptor.org), but...
2
by: kujahleague | last post by:
Dear all Is there any way to show list of filename that we have on that current directory to the user? Thanks
3
by: FinPingvin | last post by:
Hi! This code wont work properly, it returns true for showing all maps, but not any files. It is supposed to filter the JFileChooser to only show *.mp3 files. I have looked at it too long now, so...
3
dmjpro
by: dmjpro | last post by:
why these two interfaces povided as both serves same job? i mean what is the advantage of FilenameFilter over FileFilter? plz help. Kind Regards.
3
blazedaces
by: blazedaces | last post by:
I've taken a look at the class and you only seem to be able to set the file name, but not it's save type. Why is this? Here's some code if you want it, but it doesn't seem to have a method for...
0
by: jibysubhash | last post by:
hi all i have got a php code which sends attachments via mail..... Please help me how to send the file parameters to the php via http service send method.... i am placing the php and flex codes here ...
0
by: davey23crocket | last post by:
I have a C# 2.0 web service (using Visual Studio 2005.NET ) for development. And can't figure out how to have multiple out parameters in a web service if one is a List<string>. Here is the web...
17
by: chrisjohn2009 | last post by:
I need a java program to find the Depth of Inheritance of a java program source code is given as input file :) Can any1 help me out with this If so:) Thanks in Advance for Helping
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.