7 Replies Latest reply on Nov 10, 2004 12:33 PM by 843807

    AWT FileDialog to select directories

    843807
      Hello.

      I need some help. I want to create an Java AWT FileDialog to select directories. This seems to be not so easy. I have to overwrite some methods, thats what I found in other threads. But nowhere was a description, what changes had to be made.

      I would be very, very happy, if someone could help me with some code to handle this. I realy need to solve the problem.

      greetings Black
      <b>Black.78@gmx.de</b>

      PS: I don't want to use the JFileChooser and I don't want to let the user select a File and user the getDirectory() method. I wanna have a Dialog, where the user can select a folder.
        • 1. Re: AWT FileDialog to select directories
          843807
          you might want to use a FileFilter.
          have a look [url http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html]here
          especially the section where a custom filter is implemented
          • 2. Re: AWT FileDialog to select directories
            843807
            thanks, but a filter alone does not solve my problem.
            • 3. Re: AWT FileDialog to select directories
              843807
              thanks, but a filter alone does not solve my problem.
              the tutorial quite clearly shows a way to show a "directory-only" view in the file dialog. isnt that what you want?
              • 4. Re: AWT FileDialog to select directories
                843807
                Yes, that's what I want to do. But like I already wrote in my first message, I can't use the JFileChooser. And that's, what the tutorial does.
                The FileDialog also supports a FileFilter (witch does not work), I thougt you mean this one. But the FileDialog does not support a DIRECTORY_ONLY option.

                You see, it's not so simple.
                • 5. Re: AWT FileDialog to select directories
                  797177
                  Unfortunately filename filters don't work with the FileDialog class. In fact if you check the javadocs for the FileDialog class's setFilenameFilter method it states the following:

                  Filename filters do not function in Sun's reference implementation for Windows 95, 98, or NT 4.0.

                  There is a hack you can employ but it won't prevent the user from overriding the setting however it's the best you can hope to achieve (short of writing your own file dialog class). Use the FileDialog class's setFile method and pass in *. as a parameter which signifies to only display directories. As I mentioned this can be overridden by the user. See attached sample working code.
                  import java.awt.event.*;
                  import java.awt.*;
                  import java.io.*;
                  
                  public class DirOnly
                  {
                    public static void main(String args[])
                    {
                      new DirOnlyFrame();
                    }
                  }
                  
                  class DirOnlyFrame extends Frame implements ActionListener
                  {
                    MenuBar mb = new MenuBar();
                    Menu file = new Menu("File");
                    Menu edit = new Menu("Edit");
                    Menu view = new Menu("View");
                    Menu help = new Menu("Help");
                    MenuItem fileOpen = new MenuItem("Open...");
                    MenuItem separator = new MenuItem("-");
                    MenuItem fileSaveAs = new MenuItem("Save As...");
                    MenuItem editCut = new MenuItem("Cut");
                    MenuItem editCopy = new MenuItem("Copy");
                    MenuItem editPaste = new MenuItem("Paste");
                    MenuItem helpAbout = new MenuItem("About...");
                  
                    DirOnlyFrame()
                    {
                      super();
                  
                      /* Add menu items to menus */
                      file.add(fileOpen);
                      file.add(separator);
                      file.add(fileSaveAs);
                      edit.add(editCut);
                      edit.add(editCopy);
                      edit.add(editPaste);
                      help.add(helpAbout);
                  
                      /* Add menus to menubar */
                      mb.add(file);
                      mb.add(edit);
                      mb.add(view);
                      mb.add(help);
                  
                      /* Set menubar */
                      setMenuBar(mb);
                  
                      /* Add the action listeners */
                      fileOpen.addActionListener(this);
                      fileSaveAs.addActionListener(this);
                      editCut.addActionListener(this);
                      editCopy.addActionListener(this);
                      editPaste.addActionListener(this);
                      helpAbout.addActionListener(this);
                  
                      /* Add the window listener */
                      addWindowListener(new WindowAdapter()
                      {
                        public void windowClosing(WindowEvent evt)
                        {
                          dispose(); System.exit(0);
                        }
                      });
                  
                      /* Size the frame */
                      setSize(200,200);
                  
                      /* Center the frame */
                      Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
                      Rectangle frameDim = getBounds();
                      setLocation((screenDim.width - frameDim.width) / 2,(screenDim.height - frameDim.height) / 2);
                  
                      /* Show the frame */
                      setVisible(true);
                    }
                  
                    public void actionPerformed(ActionEvent evt)
                    {
                      Object obj = evt.getSource();
                  
                      if (obj == fileOpen)
                      {
                        FileDialog fd = new FileDialog(this,"Directory Only Listing");
                        fd.setFile("*.");
                        fd.setVisible(true);
                      }
                      else if (obj == fileSaveAs);
                      else if (obj == editCut);
                      else if (obj == editCopy);
                      else if (obj == editPaste);
                      else if (obj == helpAbout);
                    }
                  }
                  • 6. Re: AWT FileDialog to select directories
                    843807
                    You see, it's not so simple.
                    sorry, i should have read the original post more carefully.
                    as suggested in the previous post, there seems to be no way of doing this with FileDialog because it makes no calls to the filter before displaying.
                    some projects like [url http://nicestep.sourceforge.net]NiceStep have custom file dialogs which might be of use. otherwise there is no option but to write your own implementation.
                    • 7. Re: AWT FileDialog to select directories
                      843807
                      Yes, that's what I do now. But it takes some time.

                      @paternostro
                      Thanks for your trick. But only a Filter does't solve my problem. I neary finished a own FolderBrowser now.

                      Greetings, Black