Reputation: 21
I'm was trying to print Hello World! in the AnimationTimer just to see if it would work. There are no errors or warnings yet it doesn't work.
import javafx.animation.AnimationTimer;
public class Example {
public static void main(String[] args) throws Exception {
AnimationTimer timer = new AnimationTimer(){
@Override
public void handle(long now) {
System.out.println("Hello World!");
}
};
timer.start();
}
}
Upvotes: 1
Views: 487
Reputation: 209225
AnimationTimer
's handle()
method is invoked on the FX Application Thread, by that thread's usual pulse mechanism. In order for this to happen, the FX Application Thread must be running, and so you have to have started the FX Application Toolkit (typically by launching a JavaFX Application). Your application doesn't do this.
The following works as expected:
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.stage.Stage;
public class Example extends Application {
@Override
public void start(Stage primaryStage) {
AnimationTimer timer = new AnimationTimer(){
@Override
public void handle(long now) {
System.out.println("Hello World!");
}
};
timer.start();
}
public static void main(String[] args) {
launch(args);
}
}
Upvotes: 1