I have done the standard procedure of redirecting the console output to a textarea in JavaFX. Link of my implementation principle JavaFX: Redirect console output to TextArea that is created in SceneBuilder. However the problem is I am getting all the console output at once, i.e., the underlying program Action.run() does some work and produces console output intermediately. But in the GUI I'm getting all the output at once at the end of Action.run() execution. Is there any way to get the console output in real-time instead of the end?
@FXML
TextArea console;
private PrintStream ps;
public void initialize() {
ps = new PrintStream(new Console(console),true);
}
public class Console extends OutputStream {
private TextArea console;
public Console(TextArea console) {
this.console = console;
}
public void appendText(String valueOf) {
Platform.runLater(() -> console.appendText(valueOf));
}
public void write(int b) throws IOException {
appendText(String.valueOf((char)b));
}
}
public void buttonAction(ActionEvent event) throws Exception {
System.setOut(ps);
System.setErr(ps);
runButton.setDisable(true);
Platform.runLater(new Runnable() {
public void run() {
try {
Action action = new Action();
action.run();
} catch(Exception e) {
e.printStackTrace();
}
}
});
}