问题描述
我正在使用此类执行一些后台任务
类下载扩展任务{
protected Object call()抛出异常{
try {
updateMessage(建立连接);
DownloadHelper downloadHelper = new DownloadHelper();
downloadHelper.performTask();
返回null;
} catch(IOException | ParseException ex){
logger.error(ExceptionUtils.getStackTrace(ex));
抛出前;
}
}
}
此任务依次调用DownloadHelper执行某项任务。
class DownloadHelper {
public DownloadHelper(){
}
public void performTask(){
----
----
}
}
有没有办法从DownloadHelper类更新Task API(updateMessage())的状态消息。?
权宜之计是将下载
任务的引用作为参数传递给 DownloadHelper
构造函数。为了最小化耦合,您可以将 updateMessage()
的实现引用传递为类型
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
* /
公共类MessageTest扩展应用程序{
@Override
public void start(Stage primaryStage){
primaryStage.setTitle(MessageTest);
StackPane root = new StackPane();
Label label = new Label();
root.getChildren()。add(label);
场景场景=新场景(root,320,120);
primaryStage.setScene(scene);
primaryStage.show();
下载任务=新下载();
task.messageProperty()。addListener((Observable o) - > {
label.setText(task.getMessage());
});
Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start();
}
私有静态类下载扩展任务< String> {
@Override
protected String call()抛出异常{
updateMessage(建立连接);
DownloadHelper helper = new DownloadHelper(this :: updateMessage);
helper.performTask();
返回MessageTest;
}
@Override
protected void updateMessage(String message){
super.updateMessage(message);
}
}
私有静态类DownloadHelper {
Consumer< String>更新;
public DownloadHelper(Consumer< String> updater){
this.updater = updater;
}
public void performTask(){
updater.accept(Helper message);
}
}
public static void main(String [] args){
launch(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调用的嵌套函数更新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!