title: JFileChooser - regarding the "open" and "cancel" buttons. Java
body: <p>I am having some trouble using the JFileChooser. Whenever I run the program if I click the "cancel" button right away without selecting a file it will display "hello" and if I click open, it will not do anything. On other hand, if I select a file and click open it will start to display "Hello" (call the createFile method) and will display "hello" if I click "cancel".</p>
  
  <p>My question is how can I find out which button was clicked and to do a specific thing for each like call the die function when clicked cancel and call the createFile function when open is clicked.</p>
  
  <p>I was thinking of something like </p>
  
  <pre><code>if(e.getSource() == "Something_I_Dont_know") { do this}
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>import java.util.*;
  import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  
  
  public class Grading{
  
  public static void main(String[] arg){
  
   new MFrame();
  
  }
  
  
  }// end of class Grading
  
  class MFrame extends JFrame{
  
  private JCheckBox cum,uc,ucs;
  private JButton calc, clear, exit;
  private ButtonGroup bg;
  private JTextArea display;
  private JFileChooser input;
  
  public MFrame(){
  
      setVisible( true );
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setSize(550,550);
  
      input = new JFileChooser();
      add( input );
      input.addActionListener(
          new ActionListener(){
              public void actionPerformed( ActionEvent e ){
                  //die();
                  createFile();
              }
          }
  
      );
  
      setLayout( new FlowLayout() );
  
      pack();
  
  
  }// end of constructor
  
  public double gpa(){
   return 1.0;
  }// end of gpa method
  
  public void createFile(){
      System.out.println("Hello");
  }
  
  public void die(){
      System.exit(0);
  }
  </code></pre>
  
  <p>}// end of MFRAME CLASS</p>