The task of writing a platform independent file selection dialog would be extremely difficult, as you'd need to support Unix, Macintosh, and Windows platforms, as well as any future Java platforms. You'd also be re-inventing the wheel, as Java has its own FileDialog as part of the AWT package. Its simple to use, and provides you with an interface that looks good on any platform!

All you need to do is create a frame, create a file dialog that takes as a parameter the newly created frame, and call the dialog's show method. The dialog is modal, meaning that the application will wait for the user to select a file, or click cancel.The task of selecting files is made really simple, and you can create an open or a save dialog box by changing the parameters you give the dialog constructor.

A sample application that shows the use of the FileDialog class is given below.

import java.awt.*;

class DialogDemo 
	public static void main(String args[])
		// Create a new frame
		Frame f = new Frame();

		// Create a file dialog
		FileDialog fdialog = new FileDialog
			( f , "Open file", FileDialog.LOAD);

		// Show frame;

		// Print selection
		System.out.println ("Selection : " + fdialog.getDirectory() + fdialog.getFile());

		// Clean up and exit



Posted in: Java

Related FAQ's

Marius Ion ANGEL HOT SOFT LLC (800) 316-7677