TimerTask can't update the JavaFX Label text

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-14 01:34:09

问题


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);
    }
}

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);

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.

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.


回答1:


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());
    }
});


来源:https://stackoverflow.com/questions/20268593/timertask-cant-update-the-javafx-label-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!