I have multiple tabs and one of them is my console output. I am trying to redirect System.out
/err
to TextArea loggerPane
in that console output Tab using JavaFX. My question is how can I do that in a different thread because while it's outputting I can't navigate to the console tab.
Main App:
public initRootLayout(){
FXMLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource(RootLayout.fxml))
rootLayout = (BorderPane) loader.load();
scene = new Scene (rootLayout);
primaryStage.setScene(scene);
rootLayoutController = loader.getController();
rootLayoutController.setMainApp(this);
primaryStage.show();
}
I included the ConsoleController in RootController where I have all the tabs
@FXML private ConsoleController consoleController
RootLayoutFXML: I included the consoleFXML that uses its own controller
<Tab text="Console">
<content>
fx:include fx:id="myConsolePane" source="console.fxml"
</content>
</Tab>
ConsoleController
public class ConsoleController implements Initializable {
@FXML
private TextArea loggerPane;
public void appendText(String valueOf){
Platform.runLater(()-> loggerPane.appendText(valueOf))
}
@Override
public void initialize (URL ur, ResourceBundle rb){
OutputStream out = new OutputStream(){
@Override
public void write(int b) throws IOException{
appendText(String.valueOf((char)b));
}
};
System.setOut(out, true);
System.setErr(out, true);
}
Background Thread:
It's calling background class that interacts with a Rest API to
preform a function, sometime it outputs a long string as part of
the validation which I want to capture in the console tab.