6.7 FileDialogFileDialog is a subclass of Dialog that lets the user select files for opening or saving. You must load or save any files yourself. If used in an application or appletviewer, the FileDialog always looks like the local system's file dialog. The FileDialog is always a modal Dialog, meaning that the calling program is blocked from continuing (and cannot accept input) until the user responds to the FileDialog. Figure 6.9 shows the FileDialog component in Motif, Windows NT/95, and the Macintosh. Unlike the other Window subclasses, there is no LayoutManager for FileDialog, since you are creating the environment's actual file dialog. This means you cannot subclass FileDialog to alter its behavior or appearance. However, the class is not "final."
Netscape Navigator throws an AWTError when you try to create a FileDialog because Navigator does not permit local file system access.
FileDialog MethodsConstantsA FileDialog has two modes: one for loading a file (input) and one for saving (output). The following variables provide the mode to the constructor. The FileDialog functions the same way in both modes. The only visible difference is whether a button on the screen is labeled Load or Save. You must load or save the requested file yourself. On certain platforms there may be functional differences: in SAVE mode, the FileDialog may ask if you want to replace a file if it already exists; in LOAD mode, the FileDialog may not accept a filename that does not exist.
On some platforms in Java 1.0 getFile() returns a string that ends in .*.* (two periods and two asterisks) if the file does not exist. You need to remove the extra characters before you can create the file.
The JDK does not support the FilenameFilter with FileDialog boxes. FilenameFilter works but can't be used with FileDialog. Miscellaneous methods
java.awt.FileDialog[0,0,0x0,invalid,hidden,modal,title=Load Me,load] A FileDialog ExampleTo get a better grasp of how the FileDialog works, the following application uses a FileDialog to select a file for display in a TextArea. You can also use FileDialog to save the file back to disk. Figure 6.10 shows the application, with a file displayed in the text area; the FileDialog itself looks like any other file dialog on the run-time system. Example 6.3 shows the code.
This example can overwrite an existing file.
Example 6.3: Complete FileDialog
import java.awt.*; import java.io.*; public class FdTest extends Frame { TextArea myTextArea; Label myLabel; Button loadButton; Button saveButton; FdTest () { super ("File Dialog Tester"); Panel p = new Panel (); p.add (loadButton = new Button ("Load")); p.add (saveButton = new Button ("Save")); add ("North", myLabel = new Label ()); add ("South", p); add ("Center", myTextArea = new TextArea (10, 40)); Menu m = new Menu ("File"); m.add (new MenuItem ("Quit")); MenuBar mb = new MenuBar(); mb.add (m); setMenuBar (mb); pack(); } public static void main (String args[]) { FdTest f = new FdTest(); f.show(); } public boolean handleEvent (Event e) { if (e.id == Event.WINDOW_DESTROY) { hide(); dispose (); System.exit(0); return true; // never gets here } return super.handleEvent (e); } public boolean action (Event e, Object o) { if (e.target instanceof MenuItem) { hide(); dispose (); System.exit(0); return true; // never gets here } else if (e.target instanceof Button) { int state; String msg; if (e.target == loadButton) { state = FileDialog.LOAD; msg = "Load File"; } else {// if (e.target == saveButton) state = FileDialog.SAVE; msg = "Save File"; } FileDialog file = new FileDialog (this, msg, state); file.setFile ("*.java"); // set initial filename filter file.show(); // Blocks String curFile; if ((curFile = file.getFile()) != null) { String filename = file.getDirectory() + curFile; // curFile ends in .*.* if file does not exist byte[] data; setCursor (Frame.WAIT_CURSOR); if (state == FileDialog.LOAD) { File f = new File (filename); try { FileInputStream fin = new FileInputStream (f); int filesize = (int)f.length(); data = new byte[filesize]; fin.read (data, 0, filesize); } catch (FileNotFoundException exc) { String errorString = "File Not Found: " + filename; data = new byte[errorString.length()]; errorString.getBytes (0, errorString.length(), data, 0); } catch (IOException exc) { String errorString = "IOException: " + filename; data = new byte[errorString.length()]; errorString.getBytes (0, errorString.length(), data, 0); } myLabel.setText ("Load: " + filename); } else { // Remove trailing ".*.*" if present - signifies file does not exist if (filename.indexOf (".*.*") != -1) { filename = filename.substring (0, filename.length()-4); } File f = new File (filename); try { FileOutputStream fon = new FileOutputStream (f); String text = myTextArea.getText(); int textsize = text.length(); data = new byte[textsize]; text.getBytes (0, textsize, data, 0); fon.write (data); fon.close (); } catch (IOException exc) { String errorString = "IOException: " + filename; data = new byte[errorString.length()]; errorString.getBytes (0, errorString.length(), data, 0); } myLabel.setText ("Save: " + filename); } // Note - on successful save, text is redisplayed myTextArea.setText (new String (data, 0)); setCursor (Frame.DEFAULT_CURSOR); } return true; } return false; } } Most of this application is one long action() method that handles all the action events that take place within the Frame. The constructor doesn't do much besides arrange the display; it includes code to create a File menu with one item, Quit. This menu is visible in the upper left corner of the Frame; we'll see more about working with menus in Chapter 10, Would You Like to Choose from the Menu? We provide a main() method to display the Frame and a handleEvent() method to shut the application down if the event WINDOW_DESTROY occurs. But the heart of this program is clearly its action() method. action() starts by checking whether the user selected a menu item; if so, it shuts down the application because the only item on our menu is Quit. It then checks whether the user clicked on one of the buttons and sets the FileDialog mode to LOAD or SAVE accordingly. It then sets a default filename, *.java, which limits the display to filenames ending in .java. Next, action() shows the dialog. Because file dialogs are modal, show() blocks until the user selects a file or clicks Cancel. The next line detects whether or not getFile() returns null. A null return indicates that the user selected Cancel; in this case, the dialog disappears, but nothing else happens. We then build a complete filename from the directory name and the name the user selected. If the dialog's state is LOAD, we read the file and display it in the text area. Otherwise, the dialog's state must be SAVE, so we save the contents of the text area under the given filename. Note that we first check for the string *.* and remove it if it is present. In Java 1.1, these two lines are unnecessary, but they don't hurt, either. |
|