FileDocCategorySizeDatePackage
FileDialog.javaAPI DocJava SE 5 API15010Fri Aug 26 14:56:44 BST 2005java.awt

FileDialog

public class FileDialog extends Dialog
The FileDialog class displays a dialog window from which the user can select a file.

Since it is a modal dialog, when the application calls its show method to display the dialog, it blocks the rest of the application until the user has chosen a file.

see
Window#show
version
1.53, 12/19/03
author
Sami Shaio
author
Arthur van Hoff
since
JDK1.0

Fields Summary
public static final int
LOAD
This constant value indicates that the purpose of the file dialog window is to locate a file from which to read.
public static final int
SAVE
This constant value indicates that the purpose of the file dialog window is to locate a file to which to write.
int
mode
String
dir
String
file
FilenameFilter
filter
private static final String
base
private static int
nameCounter
private static final long
serialVersionUID
Constructors Summary
public FileDialog(Frame parent)
Creates a file dialog for loading a file. The title of the file dialog is initially empty. This is a convenience method for FileDialog(parent, "", LOAD).

param
parent the owner of the dialog
since
JDK1.1



     
        /* ensure that the necessary native libraries are loaded */
	Toolkit.loadLibraries();
        if (!GraphicsEnvironment.isHeadless()) {
            initIDs();
        }
    
	this(parent, "", LOAD);
    
public FileDialog(Frame parent, String title)
Creates a file dialog window with the specified title for loading a file. The files shown are those in the current directory. This is a convenience method for FileDialog(parent, title, LOAD).

param
parent the owner of the dialog
param
title the title of the dialog

	this(parent, title, LOAD);
    
public FileDialog(Frame parent, String title, int mode)
Creates a file dialog window with the specified title for loading or saving a file.

If the value of mode is LOAD, then the file dialog is finding a file to read, and the files shown are those in the current directory. If the value of mode is SAVE, the file dialog is finding a place to write a file.

param
parent the owner of the dialog
param
title the title of the dialog
param
mode the mode of the dialog; either FileDialog.LOAD or FileDialog.SAVE
exception
IllegalArgumentException if an illegal file dialog mode is supplied
see
java.awt.FileDialog#LOAD
see
java.awt.FileDialog#SAVE

	super(parent, title, true);
        this.setMode(mode);
	setLayout(null);
    
public FileDialog(Dialog parent)
Creates a file dialog for loading a file. The title of the file dialog is initially empty. This is a convenience method for FileDialog(parent, "", LOAD).

param
parent the owner of the dialog
exception
java.lang.IllegalArgumentException if the parent's GraphicsConfiguration is not from a screen device;
exception
java.lang.IllegalArgumentException if parent is null; this exception is always thrown when GraphicsEnvironment.isHeadless returns true
see
java.awt.GraphicsEnvironment#isHeadless
since
1.5

        this(parent, "", LOAD);
    
public FileDialog(Dialog parent, String title)
Creates a file dialog window with the specified title for loading a file. The files shown are those in the current directory. This is a convenience method for FileDialog(parent, title, LOAD).

param
parent the owner of the dialog
param
title the title of the dialog; a null value will be accepted without causing a NullPointerException to be thrown
exception
java.lang.IllegalArgumentException if the parent's GraphicsConfiguration is not from a screen device;
exception
java.lang.IllegalArgumentException if parent is null; this exception is always thrown when GraphicsEnvironment.isHeadless returns true
see
java.awt.GraphicsEnvironment#isHeadless
since
1.5

        this(parent, title, LOAD);
    
public FileDialog(Dialog parent, String title, int mode)
Creates a file dialog window with the specified title for loading or saving a file.

If the value of mode is LOAD, then the file dialog is finding a file to read, and the files shown are those in the current directory. If the value of mode is SAVE, the file dialog is finding a place to write a file.

param
parent the owner of the dialog
param
title the title of the dialog; a null value will be accepted without causing a NullPointerException to be thrown
param
mode the mode of the dialog; either FileDialog.LOAD or FileDialog.SAVE
exception
java.lang.IllegalArgumentException if an illegal file dialog mode is supplied;
exception
java.lang.IllegalArgumentException if the parent's GraphicsConfiguration is not from a screen device;
exception
java.lang.IllegalArgumentException if parent is null; this exception is always thrown when GraphicsEnvironment.isHeadless returns true
see
java.awt.GraphicsEnvironment#isHeadless
see
java.awt.FileDialog#LOAD
see
java.awt.FileDialog#SAVE
since
1.5

        super(parent, title, true);
        this.setMode(mode);
        setLayout(null);
    
Methods Summary
public voidaddNotify()
Creates the file dialog's peer. The peer allows us to change the look of the file dialog without changing its functionality.

        synchronized(getTreeLock()) {
	    if (parent != null && parent.getPeer() == null) {
		parent.addNotify();
	    }
	    if (peer == null)
	        peer = getToolkit().createFileDialog(this);
	    super.addNotify();
	}
    
java.lang.StringconstructComponentName()
Constructs a name for this component. Called by getName() when the name is null.

        synchronized (getClass()) {
	    return base + nameCounter++;
	}
    
public java.lang.StringgetDirectory()
Gets the directory of this file dialog.

return
the (potentially null or invalid) directory of this FileDialog
see
java.awt.FileDialog#setDirectory

	return dir;
    
public java.lang.StringgetFile()
Gets the selected file of this file dialog. If the user selected CANCEL, the returned file is null.

return
the currently selected file of this file dialog window, or null if none is selected
see
java.awt.FileDialog#setFile

	return file;
    
public java.io.FilenameFiltergetFilenameFilter()
Determines this file dialog's filename filter. A filename filter allows the user to specify which files appear in the file dialog window. Filename filters do not function in Sun's reference implementation for Microsoft Windows.

return
this file dialog's filename filter
see
java.io.FilenameFilter
see
java.awt.FileDialog#setFilenameFilter

	return filter;
    
public intgetMode()
Indicates whether this file dialog box is for loading from a file or for saving to a file.

return
the mode of this file dialog window, either FileDialog.LOAD or FileDialog.SAVE
see
java.awt.FileDialog#LOAD
see
java.awt.FileDialog#SAVE
see
java.awt.FileDialog#setMode

	return mode;
    
private static native voidinitIDs()
Initialize JNI field and method IDs for fields that may be accessed from C.

protected java.lang.StringparamString()
Returns a string representing the state of this FileDialog window. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null.

return
the parameter string of this file dialog window

	String str = super.paramString();
	str += ",dir= " + dir;
	str += ",file= " + file;
	return str + ((mode == LOAD) ? ",load" : ",save");
    
booleanpostsOldMouseEvents()

        return false;
    
private voidreadObject(java.io.ObjectInputStream s)
Reads the ObjectInputStream and performs a backwards compatibility check by converting either a dir or a file equal to an empty string to null.

param
s the ObjectInputStream to read

        s.defaultReadObject();

	// 1.1 Compatibility: "" is not converted to null in 1.1
	if (dir != null && dir.equals("")) {
	    dir = null;
	}
	if (file != null && file.equals("")) {
	    file = null;
	}
    
public voidsetDirectory(java.lang.String dir)
Sets the directory of this file dialog window to be the specified directory. Specifying a null or an invalid directory implies an implementation-defined default. This default will not be realized, however, until the user has selected a file. Until this point, getDirectory() will return the value passed into this method.

Specifying "" as the directory is exactly equivalent to specifying null as the directory.

param
dir the specified directory
see
java.awt.FileDialog#getDirectory

        this.dir = (dir != null && dir.equals("")) ? null : dir;
	FileDialogPeer peer = (FileDialogPeer)this.peer;
	if (peer != null) {
	    peer.setDirectory(this.dir);
	}
    
public voidsetFile(java.lang.String file)
Sets the selected file for this file dialog window to be the specified file. This file becomes the default file if it is set before the file dialog window is first shown.

Specifying "" as the file is exactly equivalent to specifying null as the file.

param
file the file being set
see
java.awt.FileDialog#getFile

        this.file = (file != null && file.equals("")) ? null : file;
	FileDialogPeer peer = (FileDialogPeer)this.peer;
	if (peer != null) {
	    peer.setFile(this.file);
	}
    
public synchronized voidsetFilenameFilter(java.io.FilenameFilter filter)
Sets the filename filter for this file dialog window to the specified filter. Filename filters do not function in Sun's reference implementation for Microsoft Windows.

param
filter the specified filter
see
java.io.FilenameFilter
see
java.awt.FileDialog#getFilenameFilter

	this.filter = filter;
	FileDialogPeer peer = (FileDialogPeer)this.peer;
	if (peer != null) {
	    peer.setFilenameFilter(filter);
	}
    
public voidsetMode(int mode)
Sets the mode of the file dialog. If mode is not a legal value, an exception will be thrown and mode will not be set.

param
mode the mode for this file dialog, either FileDialog.LOAD or FileDialog.SAVE
see
java.awt.FileDialog#LOAD
see
java.awt.FileDialog#SAVE
see
java.awt.FileDialog#getMode
exception
IllegalArgumentException if an illegal file dialog mode is supplied
since
JDK1.1

	switch (mode) {
	  case LOAD:
	  case SAVE:
	    this.mode = mode;
	    break;
	  default:
	    throw new IllegalArgumentException("illegal file dialog mode");
	}