Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4031440

FileDialog doesn't call FilenameFilter.accept()

XMLWordPrintable

    • generic, x86, sparc
    • generic, solaris_2.3, solaris_2.4, solaris_2.5, solaris_2.5.1, solaris_7, windows_95, windows_nt



      Name: mc57594 Date: 02/10/97


      Windows NT 4.0, Service Pack 2
      Pentium 120

      Add 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();
          }

      }


      company - Object Guild, Inc. , email - ###@###.###
      ======================================================================

            mmartaksunw Michael Martak (Inactive)
            mchamnessunw Mark Chamness (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: