Example: Using IFSFileDialog

Note: Read the Code example disclaimer for important legal information.
///////////////////////////////////////////////////////////////////////////////
//
// File Dialog example.
//
///////////////////////////////////////////////////////////////////////////////

import java.io.*;
import java.awt.*;
import com.ibm.as400.access.*;
import com.ibm.as400.vaccess.*;

public class FileDialogExample extends Object
{

   public static void main(String[] parameters)
   {

      System.out.println( " " );



      // if a system name was not specified, display help text and exit.

      if (parameters.length >= 1)
      {

         // The first parameter is the system that contains the files.

         String system = parameters[0];



         try
         {
             // Create an AS400 object for the server that contains the files.
             // Connect to the file server on the server.  Connect now so
             // the sign-on screen is displayed now.

             AS400 as400 = new AS400(system);
             as400.connectService(AS400.FILE);



             // Create a frame to hold the dialog.

             Frame frame = new Frame();



             // Create the file dialog object.

             IFSFileDialog fileDialog = new IFSFileDialog(frame, "File Open", as400);



             // Create the list of filters the user can choose then add the filters
             // to the dialog.

             FileFilter[] filterList = {
                new FileFilter("All files (*.*)", "*.*"),
                new FileFilter("Executables (*.exe)", "*.exe"),
                new FileFilter("HTML files (*.html)", "*.html"),
                new FileFilter("Images (*.gif)", "*.gif"),
                new FileFilter("Text files (*.txt)", "*.txt")};

             fileDialog.setFileFilter(filterList, 0);



             // Set the text for the "OK" button on the dialog.

             fileDialog.setOkButtonText("Open");



             // Set the text for the "Cancel" button on the dialog.

             fileDialog.setCancelButtonText("Cancel");



             // Set the initial directory for the dialog.

             fileDialog.setDirectory("/");



             // Display the dialog and wait until the user presses OK or Cancel

             int pressed = fileDialog.showDialog();



             // If the user pressed OK, get the fully qualified path and name
             // of the file they chose.

             if (pressed == IFSFileDialog.OK)
             {
                System.out.println("User selected: " + 
                                   fileDialog.getAbsolutePath());
             }



             // Else if the user pressed cancel, display a message.

             else if (pressed == IFSFileDialog.CANCEL)
             {
                System.out.println("User pressed cancel");
             }

             else
                System.out.println("User didn't press Open or Cancel");

          }
          catch(Exception e)
          {
             // If any of the above operations failed say the dialog operation
             // failed and output the exception.

             System.out.println("Dialog operation failed");
             System.out.println(e);
          }
      }


      // Display help text when parameters are incorrect.

      else
      {
         System.out.println("");
         System.out.println("");
         System.out.println("");
         System.out.println("Parameters are not correct.  Command syntax is:");
         System.out.println("");
         System.out.println("  FileDialogExample system");
         System.out.println("");
         System.out.println("Where");
         System.out.println("");
         System.out.println("  system = iSeries server");
         System.out.println("");
         System.out.println("For example:");
         System.out.println("");
         System.out.println("");
         System.out.println("  FileDialogExample mySystem");
         System.out.println("");
         System.out.println("");
      }

      System.exit(0);
   }
}