Basically, it doesn't work on Windows machines. The JFileDialog provides a better alternative that supports filters, if you can use the Swing component set.See Bug 4031440 for more information: http://developer.java.sun.com/developer/bugParade/bugs/4031440.html

解决方案 »

  1.   

    Bug Id  4031440
     
    Votes  488
     
    Synopsis  FileDialog doesn't call FilenameFilter.accept()
     
    Category  java:classes_awt
     
    Reported Against  1.1beta3, atlantis_dev2, 1.1.4, 1.0beta2, 3.1, 1.0.2, 1.1beta1, 1.1.2, 1.2beta3, 1.1.6, 1.2beta4, 1.1.7a, 1.2.2, kestrel, 1.1.3, 1.0.1, 1.1.1
     
    Release Fixed   
     
    State  Closed, will not be fixed
     
    Related Bugs  1243038, 4024056, 4044943, 4062249, 4067485, 4265223
     
    Submit Date  Feb 10, 1997 
     
    Description  FileDialog doesn't call FilenameFilter.accept().--------------------------------------------------------------------------------Windows NT 4.0, Service Pack 2
    Pentium 120Add a System.out.println to the accept method of Main.java
    Compile the example (from Java Class examples (from 1.0.2, in which the bug
    also manifests itself)).
    Run using java Main
    Put any filter in you wish, the accept method is never called, and the list is
    not filtered.
    Code for Main.java follows:import java.awt.*;
    import java.io.*;
    class Main extends Frame implements FilenameFilter {
        FileDialog fd;
        TextField tfDirectory = new TextField();
        TextField tfFile = new TextField();
        TextField tfFilter = new TextField();    Main() {
            super("FileDialog Example");
            add("West", new Button("Load"));
            add("East", new Button("Save"));        Panel p = new Panel();
            p.setLayout(new GridBagLayout());
            addRow(p, new Label("directory:", Label.RIGHT), tfDirectory);
            addRow(p, new Label("file:", Label.RIGHT), tfFile);
            addRow(p, new Label("filter:", Label.RIGHT), tfFilter);        add("South", p);
            pack();
            show();
        }    // Adds a row in a gridbag layout where the c2 is stretchy
        // and c1 is not.
        void addRow(Container cont, Component c1, Component c2) {
            GridBagLayout gbl = (GridBagLayout)cont.getLayout();
            GridBagConstraints c = new GridBagConstraints();
            Component comp;        c.fill = GridBagConstraints.BOTH;
            cont.add(c1);
            gbl.setConstraints(c1, c);        c.gridwidth = GridBagConstraints.REMAINDER;
            c.weightx = 1.0;
            cont.add(c2);
            gbl.setConstraints(c2, c);
        }    public boolean accept(File dir, String name) {
    System.out.println("File "+dir+" String "+name);
            if (fd.getMode() == FileDialog.LOAD) {
                return name.lastIndexOf(tfFilter.getText()) > 0;
            }
            return true;
        }    public boolean action(Event evt, Object what) {
            boolean load = "Load".equals(what);        if (load || "Save".equals(what)) {
                fd = new FileDialog(this, null,
                    load ? FileDialog.LOAD : FileDialog.SAVE);
                fd.setDirectory(tfDirectory.getText());
                fd.setFile(tfFile.getText());
                fd.setFilenameFilter(this);
                fd.show();
                tfDirectory.setText(fd.getDirectory());
                tfFile.setText(fd.getFile());            // Filter must be the same
                if (fd.getFilenameFilter() != this) {
                    throw new RuntimeException("Internal error");
                }
                return true;
            }
            return false;
        }    static public void main(String[] args) {
            new Main();
        }} 
      

  2.   

    TO 路人甲
       谢谢。不过SWING 的JFILECHOOSER不是显示不了系统图标吗。这两天我一直在找解决方法,实在不行只能这样了。咳