Chris Johnson, Index
FileDialog

/*
        Author: Chris Johnson
        johnson@dcs.gla.ac.uk
 
        Modified: 05/01/2000
 
        Description:
        Illustrates dialogs to load and save a file.
 
*/
 
 
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
 
public class FileDialogTest extends Applet
implements ActionListener{
 
        Button load = new Button("Load file...");
        Button save = new Button("Save file...");
        FileDialog dialog;
        String filename;
 
        public void init()
	{
                add(load);      // set up buttons that users presses on
                add(save);      // initial screen to then call up either
                                // the load or save dialogs as appropriate

            	load.addActionListener(this);
            	save.addActionListener(this);
        }
 
        public void actionPerformed(ActionEvent evt)
	{
 
                String arg = evt.getActionCommand();
                showStatus(null);
 
                if (arg.equals("Load file..."))
		{
                        Button button = load;
                        Frame myFrame = getFrame(button);
                        dialog = new FileDialog(myFrame, "Load A File");
                }
                else if (arg.equals("Save file..."))
		{
                        Button button = save;
                        Frame myFrame = getFrame(button);
                        dialog = new FileDialog(myFrame, "Save A File", FileDialog.SAVE);
                }
 
                dialog.show();
 
                if((filename = dialog.getFile())!= null)
                        showStatus(filename);
                else
                        showStatus("FileDialog Cancelled");
        }
 
        static Frame getFrame (Component c) {
 
                Frame frame = null;
 
                while ((c= c.getParent())!= null){
                        if (c instanceof Frame)
                        frame = (Frame) c;
                }
                return frame;
        }
 
}

forward