I need to use two threads to append two different characters to the same JavaFX TextArea. I can get one to work, but when I add in the second thread, it breaks with some very long exception. What am I doing wrong?
I looked at this question for guidance and it got me the one thread working, but not two: Display output of two different Threads in two separate JavaFx TextArea's
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
TextArea textArea = new TextArea();
textArea.setWrapText(true);
root.setCenter(textArea);
textArea.setText("");
new Thread(() -> PrintChar(textArea, 'a', 100)).start();
new Thread(() -> PrintChar(textArea, 'b', 100)).start();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
private void PrintChar(TextArea textArea, char letter, int numb)
{
for(int i = 0; i < numb; ++i)
{
textArea.appendText(letter + "");
}
}
}