title: JAVA disable button while the user has not selected a file
body: <p>I want to disable my mergeButton when the user has not selected both PDF'S.
  How would I go about doing this? I tried to do a while loop to check if <code>file1</code> and <code>file2</code> are null but the loop does not terminate. </p>
  
  <pre><code>package pdfMerge;
  
  import java.awt.BorderLayout;
  import java.awt.GridLayout;
  import java.awt.event.ActionListener;
  import java.io.File;
  import java.io.IOException;
  
  import javax.swing.JButton;
  import javax.swing.JFileChooser;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  
  import org.apache.pdfbox.exceptions.COSVisitorException;
  import org.apache.pdfbox.pdmodel.PDDocument;
  import org.apache.pdfbox.util.PDFMergerUtility;
  
  import java.awt.event.*;
  
  
  public class pdfMerger extends JFrame {
      private static final String ActionEvent = null;
      private JButton choose1, choose2, mergeButton;
      private JFileChooser fileChooser1, fileChooser2;
      private JPanel contentsPane;
      private int returnValue1, returnValue2;
      private File file1, file2;
      private String fileName1, fileName2;
      private boolean valid;
  
  
      public pdfMerger(){
          super("PDF Merger");
          setLayout(new BorderLayout());
          setSize(500, 500);
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          addComponents();
          addAction();
          setVisible(true);
  
      }
  
      public void addComponents(){
  
          contentsPane = new JPanel(new GridLayout(0, 3));
          add(contentsPane, BorderLayout.SOUTH);
  
          choose1 = new JButton("Choose 1st pdf");
          choose2 = new JButton("Choose 2nd pdf");
  
          mergeButton = new JButton("Merge");
  
  
  
          fileChooser1 = new JFileChooser();
          fileChooser2 = new JFileChooser();
  
          contentsPane.add(choose1);
          contentsPane.add(choose2);
          contentsPane.add(mergeButton);
  
      }
  
      public void addAction(){
          choose1.addActionListener(
                  new ActionListener(){
                      public void actionPerformed(ActionEvent event){
  
                                  if (event.getSource() == choose1){
                                      returnValue1 = fileChooser1.showOpenDialog(null);
                                      if (returnValue1 == JFileChooser.APPROVE_OPTION){
                                          file1 = fileChooser1.getSelectedFile();
                                          fileName1 = file1.toString();
                                          fileName1 = fileName1.replace("\\", "\\\\");
                                          System.out.println(fileName1);
  
  
                                  }
                              }
                          }
                      }
          );
          choose2.addActionListener(
                  new ActionListener(){
                      public void actionPerformed(ActionEvent event){
                                  if (event.getSource() == choose2){
                                      returnValue2 = fileChooser2.showOpenDialog(null);
                                      if (returnValue2 == JFileChooser.APPROVE_OPTION){
                                          file2 = fileChooser2.getSelectedFile();
                                          fileName2 = file2.toString();
                                          fileName2 = fileName2.replace("\\", "\\\\");
                                          System.out.println(fileName2);
                                  }
                              }
                          }
                      }
          );
  
          mergeButton.addActionListener(
                  new ActionListener(){
  
  
                      public void actionPerformed(ActionEvent event){
  
  
                          PDFMergerUtility ut = new PDFMergerUtility();
                          ut.addSource(fileName1);
                          ut.addSource(fileName2);
                          ut.setDestinationFileName("C:\\Users\\Shaheedul\\Desktop\\MergedPDF.pdf");
                          try {
                              ut.mergeDocuments();
                          } catch (Exception error){
                              System.out.println("Something went wrong!");
                          }
                      }
                  }
          );
  
  
      }
      }
  </code></pre>