TimerTask无法更新JavaFX Label文本 [英] TimerTask can't update the JavaFX Label text

查看:206
本文介绍了TimerTask无法更新JavaFX Label文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当简单的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆