1

我想在运行任务之前和之后更新状态栏标签的一些文本。这是示例代码:

    label.setText("please wait...");

    Task<Void> task = new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            veryLongOperation();
            return null;
        }
    };

    Thread thread = new Thread(task);
    thread.start();

    while (thread.isAlive()) {
        System.out.println("waiting...");
        Thread.currentThread().sleep(10);
    }

    System.out.println("work done!");
    label.setText("work done!");

在此示例中,标签的文本值无法更改...如何更新文本?

4

1 回答 1

0

您是否尝试过像这样覆盖Task#suceeded方法

Task<Void> task = new Task<Void>() {
    @Override
    protected Void call() throws Exception {
        veryLongOperation();
        return null;
    }

    @Override
    protected void succeeded() {
        System.out.println("work done!");
        label.setText("work done!");
    }
};

new Thread(task).start();
于 2013-01-04T09:54:32.463 回答