title: How to link a Jbutton with code to open a file browser to select a file?
body: <p>Trying to write a simple program and I want a button that when clicked opens up a file browser window so the user can select a file to upload to the program... </p>
  
  <p>I currently have the following code that opens up a browser window... </p>
  
  <p>How do I link this code to my existing button code so that when the button is clicked the file browser opens?</p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.io.File;
  
  import javax.swing.JFileChooser;
  import javax.swing.JFrame;
  
  public class MainClass {
  
    public static void main(String[] a) {
      JFrame frame = new JFrame("JFileChooser Popup");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
      JFileChooser fileChooser = new JFileChooser(".");
      fileChooser.setControlButtonsAreShown(false);
      frame.add(fileChooser, BorderLayout.CENTER);
  
      ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
          JFileChooser theFileChooser = (JFileChooser) actionEvent.getSource();
          String command = actionEvent.getActionCommand();
          if (command.equals(JFileChooser.APPROVE_SELECTION)) {
            File selectedFile = theFileChooser.getSelectedFile();
            System.out.println(selectedFile.getParent());
            System.out.println(selectedFile.getName());
          } else if (command.equals(JFileChooser.CANCEL_SELECTION)) {
            System.out.println(JFileChooser.CANCEL_SELECTION);
          }
        }
      };
      fileChooser.addActionListener(actionListener);
      frame.pack();
      frame.setVisible(true);
    }
  }
  </code></pre>
  
  <p>Thanks </p>
  
  <p>Can someone put this into an MVC for me? I need to do it for an exam and I have no idea how to do it...</p>
  
  <p>import java.awt.BorderLayout;</p>
  
  <p>public class Tab extends JFrame {</p>
  
  <pre><code>private JPanel contentPane;
  private JTextField textField;
  private JTextField textField_1;
  private JTextField textField_2;
  private JTextField textField_3;
  
  /**
   * Launch the application.
   */
  public static void main(String[] args) {
      EventQueue.invokeLater(new Runnable() {
          public void run() {
              try {
                  Tab frame = new Tab();
                  frame.setVisible(true);
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      });
  }
  
  {
      this.setTitle("Cipher Toolkit");
  }
  
  // set the title
  /**
   * Create the frame.
   */
  public Tab() {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setBounds(500, 400, 700, 600);
  
      JMenuBar menuBar = new JMenuBar();
      setJMenuBar(menuBar);
  
      JMenu mnFile = new JMenu("File");
      menuBar.add(mnFile);
  
      JMenuItem mntmPrint = new JMenuItem("Print");
      mnFile.add(mntmPrint);
  
      JMenuItem mntmExit = new JMenuItem("Exit");
      mnFile.add(mntmExit);
  
      JMenu mnAbout = new JMenu("About");
      menuBar.add(mnAbout);
  
      JMenuItem mntmAboutTheAuthors = new JMenuItem("About the Authors");
  
      mnAbout.add(mntmAboutTheAuthors);
  
      JMenuItem mntmProgramVersion = new JMenuItem("Program Version");
      mnAbout.add(mntmProgramVersion);
  
      JMenu mnHelp = new JMenu("Help");
      menuBar.add(mnHelp);
  
      JMenuItem mntmFaqs = new JMenuItem("FAQ's");
      mnHelp.add(mntmFaqs);
      contentPane = new JPanel();
      contentPane.setBackground(Color.LIGHT_GRAY);
      contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
      setContentPane(contentPane);
      contentPane.setLayout(null);
  
      JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
      tabbedPane.setBounds(20, 10, 655, 520);
      contentPane.add(tabbedPane);
  
      JPanel panel = new JPanel();
      panel.setBackground(Color.LIGHT_GRAY);
      tabbedPane.addTab("Ceasar Encrypt", null, panel, null);
      panel.setBorder(new TitledBorder(new LineBorder(Color.blue, 2, true)));
  
      JButton btnNewButton_1 = new JButton("LOAD INPUT FILE");
      btnNewButton_1.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
  
              JFrame frame = new JFrame("File Browser");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
              JFileChooser fileChooser = new JFileChooser(".");
              fileChooser.setControlButtonsAreShown(false);
              frame.getContentPane().add(fileChooser, BorderLayout.CENTER);
  
              ActionListener actionListener = new ActionListener() {
                  public void actionPerformed(ActionEvent actionEvent) {
                      JFileChooser theFileChooser = (JFileChooser) actionEvent
                              .getSource();
                      String command = actionEvent.getActionCommand();
                      if (command.equals(JFileChooser.APPROVE_SELECTION)) {
                          File selectedFile = theFileChooser
                                  .getSelectedFile();
                          System.out.println(selectedFile.getParent());
                          System.out.println(selectedFile.getName());
                      } else if (command
                              .equals(JFileChooser.CANCEL_SELECTION)) {
                          System.out.println(JFileChooser.CANCEL_SELECTION);
                      }
                  }
              };
              fileChooser.addActionListener(actionListener);
              frame.pack();
              frame.setVisible(true);
          }
      });
  
      panel.add(btnNewButton_1);
      btnNewButton_1.setPreferredSize(new Dimension(600, 100));
      btnNewButton_1.setFont(new Font("Arial", Font.BOLD, 40));
  
      JButton btnNewButton_2 = new JButton("SELECT OUTPUT FILE");
      btnNewButton_2.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
  
              JFrame frame = new JFrame("File Browser");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
              JFileChooser fileChooser = new JFileChooser(".");
              fileChooser.setControlButtonsAreShown(false);
              frame.getContentPane().add(fileChooser, BorderLayout.CENTER);
  
              ActionListener actionListener = new ActionListener() {
                  public void actionPerformed(ActionEvent actionEvent) {
                      JFileChooser theFileChooser = (JFileChooser) actionEvent
                              .getSource();
                      String command = actionEvent.getActionCommand();
                      if (command.equals(JFileChooser.APPROVE_SELECTION)) {
                          File selectedFile = theFileChooser
                                  .getSelectedFile();
                          System.out.println(selectedFile.getParent());
                          System.out.println(selectedFile.getName());
                      } else if (command
                              .equals(JFileChooser.CANCEL_SELECTION)) {
                          System.out.println(JFileChooser.CANCEL_SELECTION);
                      }
                  }
              };
              fileChooser.addActionListener(actionListener);
              frame.pack();
              frame.setVisible(true);
          }
      });
  
      panel.add(btnNewButton_2);
      btnNewButton_2.setPreferredSize(new Dimension(600, 100));
      btnNewButton_2.setFont(new Font("Arial", Font.BOLD, 40));
  
      JButton btnNewButton = new JButton("ENCRYPT!");
      panel.add(btnNewButton);
      btnNewButton.setPreferredSize(new Dimension(600, 100));
      btnNewButton.setFont(new Font("Arial", Font.BOLD, 40));
  
      JLabel lblSelectShiftAmount = new JLabel("SELECT SHIFT AMOUNT");
      panel.add(lblSelectShiftAmount);
  
      textField = new JTextField();
      panel.add(textField);
      textField.setColumns(10);
  
      JPanel panel_1 = new JPanel();
      panel_1.setBackground(Color.LIGHT_GRAY);
      tabbedPane.addTab("Ceasar Decrypt", null, panel_1, null);
      panel_1.setBorder(new TitledBorder(new LineBorder(Color.green, 2, true)));
  
      JButton btnNewButton_4 = new JButton("LOAD INPUT FILE");
      btnNewButton_4.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
  
              JFrame frame = new JFrame("File Browser");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
              JFileChooser fileChooser = new JFileChooser(".");
              fileChooser.setControlButtonsAreShown(false);
              frame.getContentPane().add(fileChooser, BorderLayout.CENTER);
  
              ActionListener actionListener = new ActionListener() {
                  public void actionPerformed(ActionEvent actionEvent) {
                      JFileChooser theFileChooser = (JFileChooser) actionEvent
                              .getSource();
                      String command = actionEvent.getActionCommand();
                      if (command.equals(JFileChooser.APPROVE_SELECTION)) {
                          File selectedFile = theFileChooser
                                  .getSelectedFile();
                          System.out.println(selectedFile.getParent());
                          System.out.println(selectedFile.getName());
                      } else if (command
                              .equals(JFileChooser.CANCEL_SELECTION)) {
                          System.out.println(JFileChooser.CANCEL_SELECTION);
                      }
                  }
              };
              fileChooser.addActionListener(actionListener);
              frame.pack();
              frame.setVisible(true);
  
          }
      });
      panel_1.add(btnNewButton_4);
      btnNewButton_4.setPreferredSize(new Dimension(600, 100));
      btnNewButton_4.setFont(new Font("Arial", Font.BOLD, 40));
  
      JButton btnNewButton_5 = new JButton("SELECT OUTPUT FILE");
      btnNewButton_5.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
  
              JFrame frame = new JFrame("File Browser");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
              JFileChooser fileChooser = new JFileChooser(".");
              fileChooser.setControlButtonsAreShown(false);
              frame.getContentPane().add(fileChooser, BorderLayout.CENTER);
  
              ActionListener actionListener = new ActionListener() {
                  public void actionPerformed(ActionEvent actionEvent) {
                      JFileChooser theFileChooser = (JFileChooser) actionEvent
                              .getSource();
                      String command = actionEvent.getActionCommand();
                      if (command.equals(JFileChooser.APPROVE_SELECTION)) {
                          File selectedFile = theFileChooser
                                  .getSelectedFile();
                          System.out.println(selectedFile.getParent());
                          System.out.println(selectedFile.getName());
                      } else if (command
                              .equals(JFileChooser.CANCEL_SELECTION)) {
                          System.out.println(JFileChooser.CANCEL_SELECTION);
                      }
                  }
              };
              fileChooser.addActionListener(actionListener);
              frame.pack();
              frame.setVisible(true);
  
          }
      });
      panel_1.add(btnNewButton_5);
      btnNewButton_5.setPreferredSize(new Dimension(600, 100));
      btnNewButton_5.setFont(new Font("Arial", Font.BOLD, 40));
  
      JButton btnNewButton_3 = new JButton("DECRYPT!");
      panel_1.add(btnNewButton_3);
      btnNewButton_3.setPreferredSize(new Dimension(600, 100));
      btnNewButton_3.setFont(new Font("Arial", Font.BOLD, 40));
  
      JLabel lblSelectShiftAmount_1 = new JLabel("SELECT SHIFT AMOUNT");
      panel_1.add(lblSelectShiftAmount_1);
  
      textField_1 = new JTextField();
      panel_1.add(textField_1);
      textField_1.setColumns(10);
  
      JPanel panel_2 = new JPanel();
      panel_2.setBackground(Color.LIGHT_GRAY);
      tabbedPane.addTab("Substitution Encrypt", null, panel_2, null);
      panel_2.setBorder(new TitledBorder(new LineBorder(Color.red, 2, true)));
  
      JButton btnNewButton_6 = new JButton("LOAD INPUT FILE");
      btnNewButton_6.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
  
              JFrame frame = new JFrame("File Browser");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
              JFileChooser fileChooser = new JFileChooser(".");
              fileChooser.setControlButtonsAreShown(false);
              frame.getContentPane().add(fileChooser, BorderLayout.CENTER);
  
              ActionListener actionListener = new ActionListener() {
                  public void actionPerformed(ActionEvent actionEvent) {
                      JFileChooser theFileChooser = (JFileChooser) actionEvent
                              .getSource();
                      String command = actionEvent.getActionCommand();
                      if (command.equals(JFileChooser.APPROVE_SELECTION)) {
                          File selectedFile = theFileChooser
                                  .getSelectedFile();
                          System.out.println(selectedFile.getParent());
                          System.out.println(selectedFile.getName());
                      } else if (command
                              .equals(JFileChooser.CANCEL_SELECTION)) {
                          System.out.println(JFileChooser.CANCEL_SELECTION);
                      }
                  }
              };
              fileChooser.addActionListener(actionListener);
              frame.pack();
              frame.setVisible(true);
          }
      });
      panel_2.add(btnNewButton_6);
      btnNewButton_6.setPreferredSize(new Dimension(600, 100));
      btnNewButton_6.setFont(new Font("Arial", Font.BOLD, 40));
  
      JButton btnNewButton_8 = new JButton("SELECT OUTPUT FILE");
      btnNewButton_8.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
  
              JFrame frame = new JFrame("File Browser");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
              JFileChooser fileChooser = new JFileChooser(".");
              fileChooser.setControlButtonsAreShown(false);
              frame.getContentPane().add(fileChooser, BorderLayout.CENTER);
  
              ActionListener actionListener = new ActionListener() {
                  public void actionPerformed(ActionEvent actionEvent) {
                      JFileChooser theFileChooser = (JFileChooser) actionEvent
                              .getSource();
                      String command = actionEvent.getActionCommand();
                      if (command.equals(JFileChooser.APPROVE_SELECTION)) {
                          File selectedFile = theFileChooser
                                  .getSelectedFile();
                          System.out.println(selectedFile.getParent());
                          System.out.println(selectedFile.getName());
                      } else if (command
                              .equals(JFileChooser.CANCEL_SELECTION)) {
                          System.out.println(JFileChooser.CANCEL_SELECTION);
                      }
                  }
              };
              fileChooser.addActionListener(actionListener);
              frame.pack();
              frame.setVisible(true);
  
          }
      });
      panel_2.add(btnNewButton_8);
      btnNewButton_8.setPreferredSize(new Dimension(600, 100));
      btnNewButton_8.setFont(new Font("Arial", Font.BOLD, 40));
  
      JButton btnNewButton_7 = new JButton("ENCRYPT!");
      panel_2.add(btnNewButton_7);
      btnNewButton_7.setPreferredSize(new Dimension(600, 100));
      btnNewButton_7.setFont(new Font("Arial", Font.BOLD, 40));
  
      JLabel lblSelectShiftAmount_2 = new JLabel("SELECT SHIFT AMOUNT");
      panel_2.add(lblSelectShiftAmount_2);
  
      textField_2 = new JTextField();
      panel_2.add(textField_2);
      textField_2.setColumns(10);
  
      JPanel panel_3 = new JPanel();
      panel_3.setBackground(Color.LIGHT_GRAY);
      tabbedPane.addTab("Substitution Decrypt", null, panel_3, null);
      panel_3.setBorder(new TitledBorder(
              new LineBorder(Color.yellow, 2, true)));
  
      JButton btnNewButton_10 = new JButton("LOAD INPUT FILE");
      btnNewButton_10.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
  
              JFrame frame = new JFrame("File Browser");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
              JFileChooser fileChooser = new JFileChooser(".");
              fileChooser.setControlButtonsAreShown(false);
              frame.getContentPane().add(fileChooser, BorderLayout.CENTER);
  
              ActionListener actionListener = new ActionListener() {
                  public void actionPerformed(ActionEvent actionEvent) {
                      JFileChooser theFileChooser = (JFileChooser) actionEvent
                              .getSource();
                      String command = actionEvent.getActionCommand();
                      if (command.equals(JFileChooser.APPROVE_SELECTION)) {
                          File selectedFile = theFileChooser
                                  .getSelectedFile();
                          System.out.println(selectedFile.getParent());
                          System.out.println(selectedFile.getName());
                      } else if (command
                              .equals(JFileChooser.CANCEL_SELECTION)) {
                          System.out.println(JFileChooser.CANCEL_SELECTION);
                      }
                  }
              };
              fileChooser.addActionListener(actionListener);
              frame.pack();
              frame.setVisible(true);
          }
      });
      panel_3.add(btnNewButton_10);
      btnNewButton_10.setPreferredSize(new Dimension(600, 100));
      btnNewButton_10.setFont(new Font("Arial", Font.BOLD, 40));
  
      JButton btnNewButton_11 = new JButton("SELECT OUTPUT FILE");
      btnNewButton_11.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
  
              JFrame frame = new JFrame("File Browser");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
              JFileChooser fileChooser = new JFileChooser(".");
              fileChooser.setControlButtonsAreShown(false);
              frame.getContentPane().add(fileChooser, BorderLayout.CENTER);
  
              ActionListener actionListener = new ActionListener() {
                  public void actionPerformed(ActionEvent actionEvent) {
                      JFileChooser theFileChooser = (JFileChooser) actionEvent
                              .getSource();
                      String command = actionEvent.getActionCommand();
                      if (command.equals(JFileChooser.APPROVE_SELECTION)) {
                          File selectedFile = theFileChooser
                                  .getSelectedFile();
                          System.out.println(selectedFile.getParent());
                          System.out.println(selectedFile.getName());
                      } else if (command
                              .equals(JFileChooser.CANCEL_SELECTION)) {
                          System.out.println(JFileChooser.CANCEL_SELECTION);
                      }
                  }
              };
              fileChooser.addActionListener(actionListener);
              frame.pack();
              frame.setVisible(true);
  
          }
      });
      panel_3.add(btnNewButton_11);
      btnNewButton_11.setPreferredSize(new Dimension(600, 100));
      btnNewButton_11.setFont(new Font("Arial", Font.BOLD, 40));
  
      JButton btnNewButton_9 = new JButton("DECRYPT!");
      panel_3.add(btnNewButton_9);
      btnNewButton_9.setPreferredSize(new Dimension(600, 100));
      btnNewButton_9.setFont(new Font("Arial", Font.BOLD, 40));
  
      JLabel lblSelectShiftAmount_3 = new JLabel("SELECT SHIFT AMOUNT");
      panel_3.add(lblSelectShiftAmount_3);
  
      textField_3 = new JTextField();
      panel_3.add(textField_3);
      textField_3.setColumns(10);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Nice one lid </p>