【发布时间】:2018-12-29 20:35:29
【问题描述】:
我希望每隔“n”秒打开一个对话窗口。 我尝试使用“计时器”。我有以下错误:
"Exception in threadTimer-0" java.lang.IllegalStateException: Not on FX application thread;
currentThread = Timer-0"
由此我了解到,我无法在不是 javaFX 线程的线程上创建其他窗口。
private Integer animationTime;
private void routine(Integer time) throws Exception{
animationTime = time;
Timer timer = new Timer();
timeline = new Timeline (new KeyFrame (Duration.seconds(1), evt ->
updateAnimation(time)) );
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
timer.schedule(new TimerTask() {// repeat over and over
@Override
public void run() {
alert= new Alert(Alert.AlertType.WARNING);
alert.setTitle("Alert title");
alert.setHeaderText("Alert...");
alert.setContentText("....");
alert.show();
Optional<ButtonType> result = alert.showAndWait();
if(result.get() == ButtonType.OK ){
try {
routine(time);
}
catch (Exception e){}
}
}
}, time*1000, time*1000);
}
private void updateAnimation(Integer time){
if(animationTime.equals(0)){
timeline.stop();
}
textTime.setText("Minutes: " + animationTime.toString());
animationTime -= 1;
}
我该如何解决?
2018 年 12 月 30 日更新
有一个新错误
timeline.setOnFinished((e)->{
Alert alert= new Alert(Alert.AlertType.WARNING);
alert.setTitle("Alert title");
alert.setHeaderText("Alert...");
alert.setContentText("....");
alert.show();
Optional<ButtonType> result = alert.showAndWait();
if(result.get() == ButtonType.OK ){
try {
routine(time);
}
catch (Exception ex){}
}
}
Optional<ButtonType> result = alert.showAndWait();线程“JavaFX 应用程序线程”中的异常 java.lang.IllegalStateException: showAndWait 期间不允许 动画或布局处理
【问题讨论】:
标签: java javafx timer countdown