Thursday 14 May 2015

FileDialog in Java

  This class enables you to access the user's native file opening and saving dialog boxes. It's a modal dialog box that you can create with the following two creator methods.
new FileDialog(Frame a_parent, String title)
Creates a file selection modal dialog box with the specified title.
new FileDialog(Frame a_parent, String title, int mode_flag)
Functions like the preceding method, except that you can specify, using the third parameter, whether this is a file selection or a file saving dialog box. The two constants you should use follow:
FileDialog.LOAD: Open File dialog box
FileDialog.SAVE: Save File dialog box
The general approach to using this class is to create the dialog box and then show it when you want the user to select or save a file. When the user finishes with the modal dialog box, you use the getDirectory and getFile methods to get a path to the file that's to be saved or loaded. The methods you'll find most useful for FileDialog follow.
String, getDirectory()
Returns the directory to the file the user has selected or the directory where the user wants to save a file. The string uses backslashes to separate directories and it doesn't end with a backslash. A file on disk Space inside folder Files, for example, would return /Space/Files.
String, getFile()
Returns the name of the file to be opened or the name the file is to be saved as.
FilenameFilter, getFilenameFilter()
Returns the FilenameFilter-an interface specification that enables you to filter which files appear in the dialog box-associated with the File dialog box.
int, getMode()
Returns the mode of the dialog box.
setDirectory(String default_directory)
Enables you to set the directory the user sees when the dialog box opens. Specify the directory with the same string format returned by the getDirectory method.
setFile(String a_file)
Sets the file in the dialog box.
setFilenameFilter(FilenameFilter a_filter)
Associates an instance of the FilenameFilter with the dialog box. The one method in the FilenameFilter class is called for every file, and only those that pass the test are displayed.

The applet in Listing 17.41 generates the two File dialog boxes

Listing 17.41. Generating File dialog boxes.
import java.awt.*;
import java.applet.Applet;
import java.io.*;


public class file_dialog extends Applet
{
    Frame a_window;
    FileDialog real_dialog;
    Button show_dialog;


    public void init()
    {
        //You need a Frame to make a dialog but you don't have to display it.
        a_window = new Frame("Testing Dialogs");
        //This is a dialog that lets you save files. By changing 
        //the last parameter to
        // FileDialog.LOAD you'll get a dialog that lets you open a file.
        real_dialog = new FileDialog(a_window,"Save File to:");
        show_dialog = new Button("Show File Dialog");
        add(show_dialog); 
        }
    public boolean action(Event the_event, Object the_arg) {
        //There's only one button so we just make sure that a button was clicked
        if(the_event.target instanceof Button) {
            real_dialog.show(); 
            String a_file = real_dialog.getFile();
            String a_directory = real_dialog.getDirectory();
            System.out.println(a_directory + " " + a_file);
        }
        return true;
    }
}

The FilenameFilter interface is very simple; it has only one method:
boolean, accept(File directory, String file_name)
This is called for each file. The input parameters are the directory the file is in and the filename. You can use this information to decide whether a file should be shown in the dialog box. If the method returns TRUE, the file is shown.
You can implement a FilenameFilter as shown in Listing 17.42.

Listing 17.42. Implementing a FilenameFilter.
public class my_filenameFilter implements FilenameFilter {

    my_filenameFilter() {
        ....
    }

    public boolean accept(File a_directory, String file_name) {

        //some code to decide if the file should be displayed
    }
}

FilenameFilters are useful in preventing a user from inadvertently picking the wrong file.  

 

No comments:

Post a Comment