Bobby Williams
Bobby Williams

Reputation: 31

Java Swing button freezes program

I wrote a simple application in Swing that writes text to a file. Here is my main class:

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);
            }
        });

    }
}

Here is the other class:

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);
}

}

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.

Upvotes: 0

Views: 1586

Answers (3)

Bobby Williams
Bobby Williams

Reputation: 31

I fixed my problem by using textArea.getText() instead of using scanner.

Upvotes: 0

Jandisson
Jandisson

Reputation: 380

The problem is that when you press the button, java expects to read data from System.ini (console).

Try to start your application by using the java command on a console. Then enter some text in the console after pressing the button and press enter. Your program you work.

Upvotes: 0

apetranzilla
apetranzilla

Reputation: 5919

Swing runs the actions synchronously in the same thread that's handling the GUI input and rendering. That means that when you click the button, it waits for the action listener to complete running before it goes back to handling input and drawing the GUI. In this case, it's effectively stopping the GUI from running until you type something into the console.

You can use SwingWorker to run it asynchronously so that it continues running the GUI while it runs the action.

Upvotes: 3

Related Questions