AWT FileDialog Class

FileDialog control represents a dialog window from which the user can select a file.
Following is the declaration for java.awt.FileDialog class:
public class FileDialog
   extends Dialog
Following are the fields for java.awt.Image class:
static int LOAD  -- This constant value indicates that the purpose of the file dialog window is to locate a file from which to read.
static int LOAD  -- This constant value indicates that the purpose of the file dialog window is to locate a file from which to read.
static int SAVE  -- This constant value indicates that the purpose of the file dialog window is to locate a file to which to write.
static int SAVE  -- This constant value indicates that the purpose of the file dialog window is to locate a file to which to write.
FileDialog(Dialog parent) 
Creates a file dialog for loading a file.
FileDialog(Dialog parent, String title)
 Creates a file dialog window with the specified title for loading a file.
FileDialog(Dialog parent, String title, int mode) 
Creates a file dialog window with the specified title for loading or saving a file.
FileDialog(Frame parent)
 Creates a file dialog for loading a file.
FileDialog(Frame parent, String title) 
Creates a file dialog window with the specified title for loading a file.
FileDialog(Frame parent, String title, int mode) 
Creates a file dialog window with the specified title for loading or saving a file.
void addNotify()  
Creates the file dialog's peer.
String	getDirectory()
Gets the directory of this file dialog.
String	getFile()  
Gets the selected file of this file dialog.
FilenameFilter	getFilenameFilter()
Determines this file dialog's filename filter.
int getMode()  
Indicates whether this file dialog box is for loading from a file or for saving to a file.
protected String paramString()  
Returns a string representing the state of this FileDialog window.
void setDirectory(String dir)  
Sets the directory of this file dialog window to be the specified directory.
void setFile(String file)  
Sets the selected file for this file dialog window to be the specified file.
void setFilenameFilter(FilenameFilter filter)  
Sets the filename filter for this file dialog window to the specified filter.
void setMode(int mode)
Sets the mode of the file dialog.
This class inherits methods from the following classes:
java.awt.Dialog
java.awt.Dialog
java.awt.Window
java.awt.Window
java.awt.Component
java.awt.Component
java.lang.Object
java.lang.Object
Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;

public class AwtControlDemo {

   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;

   public AwtControlDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtControlDemo  awtControlDemo = new AwtControlDemo();
      awtControlDemo.showFileDialogDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);

      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showFileDialogDemo(){
      headerLabel.setText("Control in action: FileDialog"); 

      final FileDialog fileDialog = new FileDialog(mainFrame,"Select file");
      Button showFileDialogButton = new Button("Open File");
      showFileDialogButton.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            fileDialog.setVisible(true);
            statusLabel.setText("File Selected :" 
            + fileDialog.getDirectory() + fileDialog.getFile());
         }
      });

      controlPanel.add(showFileDialogButton);
      mainFrame.setVisible(true);  
   }
}
Compile the program using command prompt. Go to D:/ > AWT  and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AwtControlDemo
Verify the following output

    13 Lectures 
     2 hours 

 EduOLC
 Print
 Add Notes
 Bookmark this page