FileFilter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SammyB
    Recognized Expert Contributor
    • Mar 2007
    • 807

    #1

    FileFilter

    Originally posted by SammyB from a previous thread
    ... 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 DataOutputStrea m), 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

    [code=java]
    import java.io.*;
    import javax.swing.*;
    class DatFilter extends javax.swing.fil echooser.FileFi lter
    {
    public boolean accept(File f)
    {
    return f.getName().end sWith(".dat");
    }
    public String getDescription ()
    {
    return "Data files (*.dat)";
    }
    }
    public class TestChooser
    {
    public static void main(String[] args)
    {
    JFileChooser fc = new JFileChooser();
    fc.setDialogTit le ("Open Payment File");
    fc.setFileSelec tionMode ( JFileChooser.FI LES_ONLY);
    fc.setCurrentDi rectory (new File ("."));
    fc.setFileFilte r(new DatFilter());
    int result = fc.showOpenDial og (null);
    if (result == JFileChooser.CA NCEL_OPTION)
    System.exit(0);
    File fFile = fc.getSelectedF ile();
    JOptionPane.sho wMessageDialog( null, fFile.getName() );
    }
    }
    [/code]
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Did you read about the addChoosableFil eFilter() method?

    kind regards,

    Jos

    Comment

    • SammyB
      Recognized Expert Contributor
      • Mar 2007
      • 807

      #3
      Originally posted by JosAH
      Did you read about the addChoosableFil eFilter() method?

      kind regards,

      Jos
      So little time, so much to read! But, a quick look says that's what I want. Thanks! --Sam

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by SammyB
        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

        Comment

        • SammyB
          Recognized Expert Contributor
          • Mar 2007
          • 807

          #5
          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.
          [code=java]
          import java.io.*;
          import javax.swing.*;
          import javax.swing.fil echooser.FileNa meExtensionFilt er;
          class AllFilter extends javax.swing.fil echooser.FileFi lter
          {
          String mDescription;
          AllFilter(Strin g Description)
          {
          mDescription = Description;
          }
          public boolean accept(File f)
          {
          return true;
          }
          public String getDescription ()
          {
          return mDescription;
          }
          }
          public class TestChooser
          {
          public static void main(String[] args)
          {
          JFileChooser fc = new JFileChooser();
          fc.setDialogTit le ("Open Payment File");
          fc.setFileSelec tionMode ( JFileChooser.FI LES_ONLY);
          fc.setCurrentDi rectory (new File ("."));
          fc.setAcceptAll FileFilterUsed( false); // So that it will appear at the end
          fc.addChoosable FileFilter(new FileNameExtensi onFilter("Text files (*.txt)","txt") );
          FileNameExtensi onFilter dat = new FileNameExtensi onFilter("Data files (*.dat)","dat") ;
          fc.addChoosable FileFilter(dat) ;
          fc.addChoosable FileFilter(new AllFilter("All files (*.*)"));
          fc.setFileFilte r(dat);
          int result = fc.showOpenDial og (null);
          if (result == JFileChooser.CA NCEL_OPTION)
          System.exit(0);
          File fFile = fc.getSelectedF ile();
          JOptionPane.sho wMessageDialog( null, fFile.getName() );
          }
          }
          [/code]

          Comment

          Working...