title: Java Swing button freezes program
body: <p>I wrote a simple application in Swing that writes text to a file. Here is my main class:</p>
  
  <pre><code>import javax.swing.JFrame;
  import javax.swing.SwingUtilities;
  
  public class WritingTextToFileApp {
  
  public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
          public void run() {
              JFrame frame = new MainFrame("Application");
  
              frame.setSize(500, 400);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setVisible(true);
              }
          });
  
      }
  }
  </code></pre>
  
  <p>Here is the other class:</p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.Container;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.io.BufferedWriter;
  import java.io.File;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.util.Scanner;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JTextArea;
  
  public class MainFrame extends JFrame {
  
  public MainFrame(String title) {
      super(title);
      //Set Layout Manager
      setLayout(new BorderLayout());
      //Create Swing Components
      JTextArea textArea = new JTextArea();
      JButton button = new JButton("Add");
      button.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              Scanner scanner = new Scanner(System.in);
              String input = scanner.nextLine();
               File file = new File("C:\\Users\\Vincent Wen\\Desktop\\Test.txt");
              try (BufferedWriter br = new BufferedWriter(new FileWriter(file))) {
                  br.write(input);
                  br.newLine();
              } catch (IOException ex) {
                  System.out.println("Unable to write to file:" + file.toString());
              }
          }
  
      });
      //Add Swing components to conent pane
      Container c = getContentPane();
      c.add(textArea, BorderLayout.CENTER);
      c.add(button, BorderLayout.SOUTH);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Whenever I press the button, the program freezes and nothing happens. Is there something wrong with the code? I am new to Swing so any help would be appreciated.</p>