Q:

JavaFX - Freezing when attempting to use BufferedReader - Unclosed?

I have been attempting for hours trying to figure this out, and it has started to annoy me.
I have a simple JavaFX application, then when a button is clicked, it 
executes Process p = pb.start() 
The command runs, and can be seen in the terminal windows, but the GUI freezes completely.
//A lot of unused imports I know...

public class GUI extends Application {

    GridPane grid;
    Scene scene; 
    Button btn1;
    Button btn2;
    TextArea text;
    Label lbl1;
    Label lbl2;

    @Override

    public void start(Stage primaryStage) throws IOException {

        primaryStage.setTitle("Flashing - ROMMING - Recovery - Rooting");
        grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setGridLinesVisible(true);
        scene = new Scene(grid,1000, 1000);
        text = new TextArea();
        text.setText("Commands will be Shown Here");

        btn1 = new Button("ADB List Devices");
        btn2 = new Button("ADB Reboot");
        btn1.setMinSize(50, 25);
        btn2.setMinSize(50, 25);
        btn1.addEventHandler(ActionEvent.ACTION, lsDevice);

        grid.add(btn1, 0, 1);
        grid.add(btn2, 0, 2);
        grid.add(text, 0, 3);

        primaryStage.setScene(scene);
        primaryStage.show();

    }
    //Logger

    //Event Handlers
    EventHandler<ActionEvent> lsDevice = new EventHandler<ActionEvent>() {
        public void handle(ActionEvent e) {
            System.out.println("Handling event " + e.getEventType());
            ProcessBuilder pb = new ProcessBuilder().command("cmd.exe");

            File log = new File("log");
            String line = null;
            try {
                 Process p = pb.start();
                 BufferedReader stdInput = new BufferedReader(new
                         InputStreamReader(p.getInputStream()));
                    // read the output from the command
                    while((line=stdInput.readLine())!=null){
                           System.out.println(line);
                    }
                }
                catch (Exception e1) {
                    e1.printStackTrace();
            }
            e.consume();
        } 
    };
}

Any help would be appreciated.
On a side note, I have a TextArea on my scene that I would like to update when the command is run.
For example, if ver button is clicked, in the TextArea I would like it to update. I have tried text.appendText(string).
Any help?
Thanks!

A:

Problem
The issue is because of processing long running task in JavaFX application thread instead of a background thread.
Solution
Move the processing work into a new Task and start the Task on the click of the Button.
Task<String> task = new Task<String>() {
     @Override 
     protected Integer call() throws Exception {
          try {
             Process p = pb.start();
             BufferedReader stdInput = new BufferedReader(new
             InputStreamReader(p.getInputStream()));
             // read the output from the command
             while((line=stdInput.readLine())!=null){
                  System.out.println(line);
                  // To update the textarea
                  updateMessage(line);
             }
          }
          catch (Exception e1) {
             e1.printStackTrace();
          }
     }
};

Inside the handler:
Thread th = new Thread(task);
th.setDaemon(true);
th.start();

The textarea, you can bind it as 
textarea.textProperty().bind().(Bindings.concat(textArea.getText()).concat(task.messageProperty()));

For more information, go through Concurrency in JavaFX.
N.B. Written the code on the fly. Might contain typos.

