0

I'm trying to launch a process and then from the output update the UI with info from it. This is my current code:

ProcessBuilder pb = new ProcessBuilder()
    .redirectErrorStream(true)
    .command(args);
Process process = pb.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;

while ((line = br.readLine()) != null)
{
    System.out.println(line.trim());
    // Trying to update the UI here
}

The provlem is that the line gets outputted to the console, but the UI freezes and doesn't get updated. I assume it's because of the while loop, but how do I fix it?

Thanks!

kraxie
  • 11
  • 2
  • 4
  • 3
    The UI must be updated in the application thread only. Are you using [Platform.runLater](http://docs.oracle.com/javase/8/javafx/api/javafx/application/Platform.html#runLater-java.lang.Runnable-) to update your UI? Alternatively, is your process code running in a [Task](http://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html)? – VGR May 25 '17 at 13:04
  • If the UI is freezing, it is because you are running the entire process on the FX Application Thread (preventing that thread from doing anything else, such as rendering the UI). See https://stackoverflow.com/a/35470182/2189127 or https://stackoverflow.com/questions/15746474/javafx-how-to-bind-two-values (among many other relevant posts) – James_D May 25 '17 at 13:28
  • A combination of running all of the code inside of a Task and updating the UI through the Platform.runLater inside of it seems to have done it, thanks! – kraxie May 25 '17 at 13:53

0 Answers0