Chapter: Java The Complete Reference - The Java Library - Using AWT Controls, Layout Managers, and Menus

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

FileDialog - AWT

Java provides a built-in dialog box that lets the user specify a file. To create a file dialog box, instantiate an object of type FileDialog. This causes a file dialog box to be displayed.

FileDialog


Java provides a built-in dialog box that lets the user specify a file. To create a file dialog box, instantiate an object of type FileDialog. This causes a file dialog box to be displayed.

 

Usually, this is the standard file dialog box provided by the operating system. Here are three FileDialog constructors:

 

FileDialog(Frame parent) FileDialog(Frame parent, String boxName)

FileDialog(Frame parent, String boxName, int how)

 

Here, parent is the owner of the dialog box. The boxName parameter specifies the name displayed in the box’s title bar. If boxName is omitted, the title of the dialog box is empty. If how is FileDialog.LOAD, then the box is selecting a file for reading. If how is

 

FileDialog.SAVE, the box is selecting a file for writing. If how is omitted, the box is selecting a file for reading.

FileDialog provides methods that allow you to determine the name of the file and its path as selected by the user. Here are two examples:

 

String getDirectory( ) String getFile( )

 

These methods return the directory and the filename, respectively. The following program activates the standard file dialog box:

 

/* Demonstrate File Dialog box.

 

This is an application, not an applet.

 

*/

 

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

 

// Create a subclass of Frame.

class SampleFrame extends Frame {

 

SampleFrame(String title) { super(title);

 

// remove the window when closed

addWindowListener(new WindowAdapter() {

 

public void windowClosing(WindowEvent we) { System.exit(0);

 

}

 

});

 

}

 

}

 

// Demonstrate FileDialog.

class FileDialogDemo {

 

public static void main(String args[]) { // create a frame that owns the dialog

 

Frame f = new SampleFrame("File Dialog Demo"); f.setVisible(true);

 

f.setSize(100, 100);

 

FileDialog fd = new FileDialog(f, "File Dialog");

 

fd.setVisible(true);

 

}

 

}

The output generated by this program is shown here. (The precise configuration of the dialog box may vary.)


One last point: Beginning with JDK 7, you can use FileDialog to select a list of files. This functionality is supported by the setMultipleMode( ), isMultipleMode( ), and getFiles( ) methods.

 

 


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.