1

Suppose I have a bunch of files named as such:

10011-1-chassis.EDRW
10011-2-front.EDRW
10011-3-rear.EDRW
20011-1-chassis.EDRW
20011-2-front.EDRW
20011-3-back.EASM
20011-3-cover.EASM

If I want only the 20011-x files then how would I list them all and then only the appropriate files so that I could present the filenames in a JOptionPane for the user to choose from a dropdown which file they were interested in?

davidahines
  • 3,976
  • 16
  • 53
  • 87

3 Answers3

6

Write a java.io.FilenameFilter that uses a regular expression that filters out unacceptable file names.

I recommend a regex because I'm assuming that you'll want to change the pattern dynamically. It's not overkill in that case. A hard-wired solution for the example you cite doesn't seem that useful to me. I'm assuming that users will want to tell you how they want to change the pattern by specifying it in your UI..

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • The users in this case will only be able to open two filetypes as this section of the app cannot allow any files that aren't edrawings to be opened. – davidahines May 16 '11 at 20:06
2

Use a JFileChooser instead of the JOptionPane with list. E.G.

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.io.File;

class ShowPrefixedDrawings {

    public static void main(String[] args) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                JFileChooser chooser = new JFileChooser();
                chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                chooser.setMultiSelectionEnabled(true);
                chooser.setAcceptAllFileFilterUsed(false);
                String prefix = JOptionPane.showInputDialog(
                    null, "Prefix", "D");
                chooser.setFileFilter(
                    new PrefixDrawingFileFilter(prefix));
                int result = chooser.showOpenDialog(null);
                if (result==JFileChooser.APPROVE_OPTION) {
                    File[] files = chooser.getSelectedFiles();
                    for (File file : files) {
                        System.out.println(file.getName());
                    }
                }
                System.exit(0);
            }
        });
    }
}

class PrefixDrawingFileFilter extends FileFilter {

    String prefix;
    String[] suffixes = {"dwg", "dxf", "DWG", "DXF"};

    PrefixDrawingFileFilter(String prefix) {
        this.prefix = prefix;
    }

    public boolean accept(File f) {
        if (f.isDirectory()) return true;

        String name = f.getName();
        if ( name.startsWith(prefix) ) {
            for (String type : suffixes) {
                if (name.endsWith(type)) return true;
            }
        }

        return false;
    }

    public String getDescription() {
        return "eDrawings Viewer files starting with " + prefix;
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • The only problem is that this will be used to launch an edrawings file and using a JFileChooser will then let the user pick any file at all to open won't it? Meaning that they could open anything they wanted? – davidahines May 16 '11 at 18:08
  • 1
    @dah: "..open anything they wanted?" What happened when you tried it? AFAIU the only way to provide false information back is to type bogus file names into the 'File Name:' field of the file chooser. Even then a bit of quick checking on the other side would weed out the nonsense users. Pop a `JOptionPane` pointing out that they are a numb-skull & they will probably select files with the mouse thereafter. ;) BTW - what file extension is used for edrawings? – Andrew Thompson May 16 '11 at 18:23
  • Hahahahahaha, EDRW and EASM are the extensions used. This is to be displayed on a touchscreen running a remotedesktop to a vm and the file that they choose isn't going to be opened by the application itself but instead opened in edrawings through Desktop.open(file) and using the default file association for edrawings it will automagically open in edrawings, I was concerned that a JFileChooser would allow them to open images or other apps, slowing the VM. Also, theres no keyboard so that's not a concern. – davidahines May 16 '11 at 18:31
  • 1
    @dah: "EDRW and EASM are the extensions used." Huh. I just surfed over to the site and got the impression the types were DWG & DXF. No matter, change those as needed. – Andrew Thompson May 16 '11 at 18:40
  • http://tinyurl.com/65e46v4 I think that there are multiple filetypes for solidworks. These in particular are EDRW and EASM http://tinyurl.com/5sjs3qp DXF and DWG are also extensions we deal with but not for this particular app. – davidahines May 16 '11 at 18:43
  • This is a pretty perfect answer AT, thanks for all your help. – davidahines May 16 '11 at 18:47
  • Is it possible to limit a JFilechooser to one directory? – davidahines May 16 '11 at 18:53
  • 1
    http://stackoverflow.com/questions/32529/how-do-i-restrict-jfilechooser-to-a-directory – davidahines May 16 '11 at 18:55
  • Disregard that. Thanks for all the help. – davidahines May 16 '11 at 18:56
  • 1
    @dah: "Is it possible to limit a JFilechooser to one directory?" Besides the `FileSystemView` advice in that thread, you might also want to remove the `if (f.isDirectory()) return true;` line in the file filter. – Andrew Thompson May 16 '11 at 19:09
  • @ Andrew Thompson: I think that may be the route I go. – davidahines May 16 '11 at 19:16
0

Use filename.startsWith ("20011-") as your filtering predicate.

Robin Green
  • 32,079
  • 16
  • 104
  • 187