1

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.   
Moe
  • 1,427
  • 4
  • 34
  • 54
  • 2
    Can you show what your background thread is doing and how you are starting it, etc? This looks fundamentally correct, but it has the potential to create a huge number of `Runnable`s to execute on the FX Application Thread (one per character!!!). If that's the problem, and you can't move the `Platform.runLater(...)` to a point in the code where it is dealing with data in greater bulk, you may need to implement some kind of buffer that you periodically empty to the text area. – James_D Aug 02 '17 at 12:57
  • Hello James, sorry for my delayed response. I updated the question with more detail, I hope this can help you to point me into the right direction. – Moe Aug 02 '17 at 23:58

0 Answers0