从 JavaFX 中的 Task API 调用的嵌套函数更新标签 [英] Update label from nested function called from Task API in JavaFX

查看:26
本文介绍了从 JavaFX 中的 Task API 调用的嵌套函数更新标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这个类执行一些后台任务

class 下载扩展任务{受保护的对象调用()抛出异常{尝试 {updateMessage("建立连接");DownloadHelper downloadHelper = new DownloadHelper();下载Helper.performTask();返回空;} catch (IOException | ParseException ex) {logger.error(ExceptionUtils.getStackTrace(ex));扔前;}}}

这个 Task 依次调用 DownloadHelper 来执行一些任务.

class DownloadHelper{公共下载助手(){}public void performTask(){--------}}

有没有办法从 DownloadHelper 类更新 Task API (updateMessage())的状态消息.?

解决方案

权宜之计是将 Download 任务的引用作为参数传递给 DownloadHelper 构造函数.为了最小化耦合,您可以改为传递对 updateMessage() 实现的引用作为

import java.util.function.Consumer;导入 javafx.application.Application;导入 javafx.beans.InvalidationListener;导入 javafx.beans.Observable;导入 javafx.concurrent.Task;导入 javafx.scene.Scene;导入 javafx.scene.control.Label;导入 javafx.scene.layout.StackPane;导入 javafx.stage.Stage;/*** @see https://stackoverflow.com/q/45708923/230513*/公共类 MessageTest 扩展应用程序 {@覆盖公共无效开始(阶段primaryStage){primaryStage.setTitle("MessageTest");StackPane root = new StackPane();标签标签=新标签();root.getChildren().add(label);场景场景 = 新场景(root, 320, 120);primaryStage.setScene(场景);primaryStage.show();下载任务 = new Download();task.messageProperty().addListener((Observable o) -> {label.setText(task.getMessage());});线程线程=新线程(任务);线程.setDaemon(真);线程开始();}私有静态类下载扩展任务{@覆盖受保护的字符串调用()抛出异常{updateMessage("建立连接");DownloadHelper helper = new DownloadHelper(this::updateMessage);helper.performTask();返回消息测试";}@覆盖受保护的无效更新消息(字符串消息){super.updateMessage(message);}}私有静态类 DownloadHelper {消费者<字符串>更新程序;public DownloadHelper(Consumer updater) {this.updater = 更新程序;}公共无效执行任务(){updater.accept("帮助消息");}}公共静态无效主(字符串 [] args){发射(参数);}}

I am performing some background task using this class

class Download extends Task{
     protected Object call() throws Exception {
        try {
            updateMessage("Establishing Connection");
            DownloadHelper downloadHelper = new DownloadHelper();
            downloadHelper.performTask();
            return null;
        } catch (IOException | ParseException ex) {
            logger.error(ExceptionUtils.getStackTrace(ex));
            throw ex;
        }
    }
}

This Task in turn calls DownloadHelper to perform some task.

class DownloadHelper{
    public DownloadHelper(){
    }

    public void performTask(){
    ----
    ----
    }
}

Is there a way to update the status message of the Task API (updateMessage()) from the DownloadHelper class.?

解决方案

The expedient approach is to pass a reference to the Download task as a parameter to the DownloadHelper constructor. To minimize coupling, you can instead pass a reference to your implementation of updateMessage() as a parameter of type Consumer, "an operation that accepts a single input argument and returns no result."

DownloadHelper helper = new DownloadHelper(this::updateMessage);

Your helper's implementation of performTask() can then ask the updater to accept() messages as needed.

Consumer<String> updater;

public DownloadHelper(Consumer<String> updater) {
    this.updater = updater;
}

public void performTask() {
    updater.accept("Helper message");
}

A related example is seen here.

import java.util.function.Consumer;
import javafx.application.Application;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 * @see https://stackoverflow.com/q/45708923/230513
 */
public class MessageTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("MessageTest");
        StackPane root = new StackPane();
        Label label = new Label();
        root.getChildren().add(label);
        Scene scene = new Scene(root, 320, 120);
        primaryStage.setScene(scene);
        primaryStage.show();
        Download task = new Download();
        task.messageProperty().addListener((Observable o) -> {
            label.setText(task.getMessage());
        });
        Thread thread = new Thread(task);
        thread.setDaemon(true);
        thread.start();
    }

    private static class Download extends Task<String> {

        @Override
        protected String call() throws Exception {
            updateMessage("Establishing connection");
            DownloadHelper helper = new DownloadHelper(this::updateMessage);
            helper.performTask();
            return "MessageTest";
        }

        @Override
        protected void updateMessage(String message) {
            super.updateMessage(message);
        }
    }

    private static class DownloadHelper {

        Consumer<String> updater;

        public DownloadHelper(Consumer<String> updater) {
            this.updater = updater;
        }

        public void performTask() {
            updater.accept("Helper message");
        }
    }

    public static void main(String[] args) {
        launch(args);
    }

}

这篇关于从 JavaFX 中的 Task API 调用的嵌套函数更新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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