I have created a JavaFX application which I would want to have the following functionality:
Start running - when running appendText() to a JavaFX TextArea every 2 seconds, and then randomly stop everything and show an Alert.Error.
This is really basic. I searched a bit on Stack Overflow and I could not find anything. If this has already been answered please point me to it.
And to explain my problem a bit better, here is some code and some screenshots.
package com.example.threadstestingplatformrunlater;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.IOException;
import static java.lang.Thread.sleep;
public class HelloApplication extends Application {
private static int counter;
private TextArea textArea;
@Override public void start(Stage stage) throws IOException {
VBox vBox = new VBox();
textArea = new TextArea();
vBox.getChildren().add(textArea);
Scene scene = new Scene(vBox);
vBox.setStyle(ThemeClass.DARK_THEME);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
System.out.println("Thread Name: " + Thread.currentThread().getName() );
Thread thread = new Thread(this::run);
thread.start();
}
private void functionContainingRunLater() throws InterruptedException {
System.out.println("Thread Name: " + Thread.currentThread().getName() );
continuouslyWrite(textArea);
}
public static void continuouslyWrite(TextArea textArea) throws InterruptedException {
for (int i = 0; i < 20; i++) {
if(i == 10) {
Platform.runLater(()-> {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.show();
});
}
System.out.println(counter);
textArea.appendText(Thread.currentThread().getName() + ": " + counter+ "\n");
counter++;
if (counter > 500) {
break;
}
sleep(200);
}
}
public static void main(String[] args) {
launch();
}
private void run() {
try {
functionContainingRunLater();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
This code runs and it is writing lines on the TextArea as it goes. In real time. I had quite some issues getting it this far - more or less, it would only write after everything has finished.
Now I would like to stop it when i == 10
, but I only show the Alert.Message and then it goes until the end. I would like for the code to pause. How could I achieve that?
I have tried with:
public static void continuouslyWrite(TextArea textArea) throws InterruptedException {
for (int i = 0; i < 20; i++) {
if(i == 10) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.show();
}
System.out.println(counter);
textArea.appendText(Thread.currentThread().getName() + ": " + counter+ "\n");
counter++;
if (counter > 500) {
break;
}
sleep(200);
}
}
But in this case, I get the error of not being on JavaFX thread.
Exception in thread "Thread-3" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-3
Could you help me understand how could I achieve this? Is what I want to do even possible? Somehow I can not get my head around it.