2017-08-16 94 views
0

从任务API叫我使用这个类更新标签JavaFX中

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

这个任务依次调用DownloadHelper执行某些任务执行一些后台任务。

class DownloadHelper{ 
    public DownloadHelper(){ 
    } 

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

有没有办法从DownloadHelper类更新任务API(updateMessage())的状态消息。

+0

作为参数传递给封闭'Task'一个参考'DownloadHelper'构造? – trashgod

+0

这会导致高度的耦合......我想知道是否可以使用javafx中的Property来完成 –

+0

将引用封闭的Task的'message'属性作为参数传递给DownloadHelper构造函数? – trashgod

回答

1

方便的方法是将对Download任务的引用作为参数传递给构造函数DownloadHelper。为了最小化耦合,您可以将updateMessage()的实现的引用作为参数​​传递给“接受单个输入参数且不返回结果的操作”。然后

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

你的助手的实施performTask()可以根据需要要求updateraccept()消息。

Consumer<String> updater; 

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

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

可以看到一个相关的例子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); 
    } 

}