Javafx任务 - 从方法

时间:2015-12-18 13:39:13

标签: javafx concurrency progress-bar

在JavaFX应用程序中,我希望根据我在其他类中实现的一些工作逻辑更新状态栏。

我无法弄清楚如何将我传递工作逻辑的愿望与方法结合起来(而不是将其写入任务中)并了解工作进度百分比。

这是具有任务:

的控制器的示例
public class FXMLDocumentController implements Initializable {

    @FXML private Label label;    
    @FXML ProgressBar progressBar;

    @FXML
    private void handleButtonAction(ActionEvent event) {

        Service<Void> myService = new Service<Void>() {

            @Override
            protected Task<Void> createTask() {
                return new Task<Void>() {

                    @Override
                    protected Void call() throws Exception {
                        try {
                            DatabaseFunctionality.performWorkOnDb();

                            //updateProgress(1, 1);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
                        }

                        return null;
                    }
                }; 
            }            
        };

        progressBar.progressProperty().bind(myService.progressProperty());
        myService.restart();
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }        
}

这是助手类:

public class DatabaseFunctionality {

    public static void performWorkOnDb () throws InterruptedException {
        for (int i = 1; i <= 100; i++) {
            System.out.println("i=" + i);
            Thread.sleep(100);

            //Update progress
        }        
    }    
}

谢谢

1 个答案:

答案 0 :(得分:5)

你有几个选择。一个是按照Uluk的建议并在 let editController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("StoryboardID") as! MyEditViewController 类中公开一个可观察的属性:

DatabaseFunctionality

现在在您的public class DatabaseFunctionality { private final ReadOnlyDoubleWrapper progress = new ReadOnlyDoubleWrapper(); public double getProgress() { return progressProperty().get(); } public ReadOnlyDoubleProperty progressProperty() { return progress ; } public void performWorkOnDb() throws Exception { for (int i = 1; i <= 100; i++) { System.out.println("i=" + i); Thread.sleep(100); progress.set(1.0*i / 100); } } } 中,您可以观察该属性并更新任务的进度:

Task

另一个选项(如果您不希望您的数据访问对象依赖于JavaFX属性API)是为数据访问对象传递回调以更新进度。 BiConsumer<Integer, Integer>适用于此:

Service<Void> myService = new Service<Void>() {

    @Override
    protected Task<Void> createTask() {
        return new Task<Void>() {

            @Override
            protected Void call() throws Exception {
                try {
                    DatabaseFunctionality dbFunc = new DatabaseFunctionality();
                    dbFunc.progressProperty().addListener((obs, oldProgress, newProgress) -> 
                        updateProgress(newProgress.doubleValue(), 1));

                    dbaseFunc.performWorkOnDb();

                } catch (InterruptedException ex) {
                    Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
                }

                return null;
            }
        }; 
    }            
};

然后

public class DatabaseFunctionality {

    private BiConsumer<Integer, Integer> progressUpdate ;

    public void setProgressUpdate(BiConsumer<Integer, Integer> progressUpdate) {
        this.progressUpdate = progressUpdate ;
    }

    public void performWorkOnDb() throws Exception {
        for (int i = 1; i <= 100; i++) {
            System.out.println("i=" + i);
            Thread.sleep(100);

            if (progressUpdate != null) {
                progressUpdate.accept(i, 100);
            }
        }        
    }   
}