13.11. The FileDialog Class

A FileDialog is a type of modal dialog that is used to load or save files. Use FileDialog with applications; normal applets cannot access the local disk. Although a FileDialog is technically a Container, it cannot hold any other components and has no layout manager. Here are the basic steps for creating and using a FileDialog.

// Make the FileDialog.
FileDialog fileLoader =
  new FileDialog(frame, title, FileDialog.LOAD);
FileDialog fileSaver =
  new FileDialog(frame, title, FileDialog.SAVE);
//Set a default file or file type.
fileLoader.setFile("*.txt");
// Pop up the FileDialog.
fileLoader.show();
// Look up the filename chosen.
String filename = fileLoader.getFile();

Example: Displaying Files in a TextArea

Listing ...

Get Core Web Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.