Determining If the Approve or Cancel Button Was Clicked in a JFileChooser Dialog - Java Swing

Java examples for Swing:JFileChooser

Description

Determining If the Approve or Cancel Button Was Clicked in a JFileChooser Dialog

Demo Code

import javax.swing.JFileChooser;

public class Main {
  public static void main(String[] argv) {
    JFileChooser chooser = new JFileChooser();
    int result = chooser.showOpenDialog(null);

    // Determine which button was clicked to close the dialog
    switch (result) {
    case JFileChooser.APPROVE_OPTION:
      // Approve (Open or Save) was clicked
      break;/*from  w  ww  .  jav a  2 s .  c om*/
    case JFileChooser.CANCEL_OPTION:
      // Cancel or the close-dialog icon was clicked
      break;
    case JFileChooser.ERROR_OPTION:
      // The selection process did not complete successfully
      break;
    }
  }

}

Related Tutorials