I am creating a GUI that is running a program that runs tests and writes their output as text to the console. I created a table the allows the user to select the tests they want to run, when the user clicks "run" then it goes through the table and runs the tests selected and is supposed to write the output to a textArea. When I run the program the textArea wouldn't update until it has run all the tests but I need it to update as the test outputs the text.
From what I've read I need to create multiple Threads because running the program and writing to the textArea are both processes. I don't really have a solid grasp of how the Threading works but I've tried using a StringBuffer so the output of the test can be stored and used by the second Thread I created.
public void runTest(ArrayList<String> arr) throws InterruptedException{
StringBuffer sb = new StringBuffer();
Thread t = new Thread(() -> {
try {
ProcessBuilder builder = new ProcessBuilder(arr);
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while((line = r.readLine()) != null){
sb.append(line).append("\n");
}
System.out.println(line);
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
});
Thread t2 = new Thread(()->{
String line = sb.toString();
System.out.println(line);
txtOutputArea.appendText(line + "\n");
});
t.start();
t2.start();
t.join();
t2.join();
}
I'm printing the text to the console and it works but for some reason there is no output to the textArea.