TimerTask无法更新JavaFX Label文本 [英] TimerTask can't update the JavaFX Label text
问题描述
我有一个相当简单的JavaFX GUI应用程序有一个标签,显示在某个动作开始之前剩余的时间。为此,我创建了一个DownloadTimer类,如下所示:
I have a fairly simple JavaFX GUI application which has an label that shows how much time is left until a certain action starts. To achieve this, I've created a DownloadTimer class as shown below:
public class DownloadTimer(){
private int minutes;
private int seconds;
private Timer innerTimer = new Timer();
private TimerTask innerTask;
private boolean isActive;
public DownloadTimer(int minutes, int seconds) {
if (seconds > 60) {
int minToAdd = seconds / 60;
this.minutes = minutes;
this.minutes += minToAdd;
this.seconds = seconds % 60;
} else {
this.minutes = minutes;
this.seconds = seconds;
}
}
public void start() {
innerTask = new TimerTask() {
@Override
public void run() {
isActive = true;
System.out.println(getTime());
if (seconds == 0 && minutes > 0){
minutes -= 1;
seconds = 59;
} else if (seconds == 0 && minutes == 0){
isActive = false;
innerTimer.cancel();
innerTimer.purge();
System.out.println("DownloadTimer DONE");
} else {
seconds -= 1;
}
}
};
innerTimer.scheduleAtFixedRate(innerTask, 0, 1000);
}
}
然后,我正在创建DownloadTimer对象从我的Main(JavaFX)类开始倒计时:
And then, I'm creating the DownloadTimer object and starting the countdown from my Main (JavaFX) class:
/*
code omitted for better readability
*/
downloadTimer = new DownloadTimer(0, 5);
// label gets the .getTime() value, which returns a formatted String like "00:05", "00:04", etc.
lblTimer.setText( downloadTimer.getTime() );
// start the countdown
downloadTimer.start();
// create a new timer which checks if the downloadTimer is still counting
final Timer timer = new Timer();
TimerTask timerTask = new TimerTask(){
@Override
public void run(){
if (downloadTimer.getIsActive() == false){
timer.cancel();
timer.purge();
System.out.println("GUI timer DONE");
} else {
// if it's still running, then continuously update the label's text
lblTimer.setText( downloadTimer.getTime() );
// this is where I get the error described below
}
}
};
// repeat after 1000ms
timer.scheduleAtFixedRate(timerTask, 0, 1000);
我遇到的问题是我无法用<$设置标签文字来自Main类的c $ c> lblTimer.setText(downloadTimer.getTime()); ,我得到的错误是 TimerThread.run()行:不可用[本地变量不可用]
如此处所见。
The problem I'm encountering with this is that I can't set the label text with lblTimer.setText( downloadTimer.getTime() );
from the Main class, and the error I'm getting is TimerThread.run() line: not available [local variables unavailable]
as seen here.
我读过 ScheduledThreadPoolExecutor 和 Java Timer vs ExecutorService ,但我很好奇是否可以使用两个独立的定时器和TimerTasks来完成。任何帮助和/或提示将不胜感激。
I've read about ScheduledThreadPoolExecutor and Java Timer vs ExecutorService, but I'm curious if this can be done using two separate Timers and TimerTasks. Any help and/or tips would be greatly appreciated.
推荐答案
我很惊讶你没有看到异常。要从单独的线程更新标签,需要安排在FX线程中运行更新:
I'm surprised you're not seeing an exception. To update a label from a separate thread, one needs to schedule an update to be run in the FX thread:
Platform.runLater(new Runnable() {
public void run() {
lblTimer.setText(downloadTimer.getTime());
}
});
这篇关于TimerTask无法更新JavaFX Label文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!