Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
521 views
in Technique[技术] by (71.8m points)

properties - Update label from nested function called from Task API in JavaFX

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.?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

image

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...