[ Back | Previous | Next ]

Using the FileDialog component.

Package:
java.awt.*
Product:
JDK
Release:
1.1.x
Related Links:
Color
Cursor
FileDialog
Font
FontMetrics
Frame
general
Image
LayoutManager
Menu
ScrollPane
Comment:
We all know the standard way various os have the possiblity to graphically choose a file from a file list. Java calls this the FileDialog. But how to use the file dialog?

The example here creates:

  1. a menuitem
  2. a action adapter which listens for a selection;
  3. gets a filedialog from the parent this$1 (Frame);
  4. loads the filedialog;
  5. loads the file by means of name stored in fd.getFile();
import java.awt.*;
import java.awt.event.*;

    /*
    * @param title frame title
    */
    public GUI (String title) {
      super( title );
      setLayout( new BorderLayout() );
      setBackground( Color.white );

      // load default filemenu
      filemenu = new MenuList("File", filemenuobjects, 
        new ActionAdapter() 
        {
                                public void actionPerformed(ActionEvent ae) 
          {
             //-----------OPEN
            if ( ae.getActionCommand().equals("Open") ) {
              if (fd== null) 
              {     
                fd = new FileDialog( this$1, "FileDialog");
              }      
              fd.setMode( FileDialog.LOAD );
              fd.show();
              System.out.println("Menu -> File -> Open");  
              if ( fd.getDirectory() != null && fd.getFile() != null) 
              {
                filename.setText( fd.getDirectory() + fd.getFile() );
              } 
              else 
              {
                filename.setText("none");
              }
              ffile = new File( fd.getDirectory() + File.separator + fd.getFile());
            }
          }
          );
        }
      } 
    }
..
}