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

    AWT FileDialog to select directories


      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

      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
          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
            thanks, but a filter alone does not solve my problem.
            • 3. Re: AWT FileDialog to select directories
              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
                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
                  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...");
                      /* Add menu items to menus */
                      /* Add menus to menubar */
                      /* Set menubar */
                      /* Add the action listeners */
                      /* Add the window listener */
                      addWindowListener(new WindowAdapter()
                        public void windowClosing(WindowEvent evt)
                          dispose(); System.exit(0);
                      /* Size the frame */
                      /* 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 */
                    public void actionPerformed(ActionEvent evt)
                      Object obj = evt.getSource();
                      if (obj == fileOpen)
                        FileDialog fd = new FileDialog(this,"Directory Only Listing");
                      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
                    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
                      Yes, that's what I do now. But it takes some time.

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

                      Greetings, Black