执行public static方法并捕获返回值,可能吗?

时间:2016-05-29 15:20:34

标签: java javafx

我从AddTask调用静态方法ScanSystem()。这里我想声明一个字符串变量,这似乎有效,但AddTask()内的所有内容都在完成方法ScanSystem()后发生,为什么? 完成AddTask()

后,用户界面无法更新

ScanSystem()

private void ScanSystem() {
    double startScanTime = System.currentTimeMillis();
    //Preparing Event
    String startupTaskText = "System Scan has started!";
    double startUpProgress = 0;
    String taskID = App.AddTask(startupTaskText, startUpProgress);

    String [][] snapshotArray = manager.getSystemSnapshotsScanSystem(); //Get snapshots from Manager in Array
    tableScanSystemForSnapshots.setItems(getSnapshot(snapshotArray)); //push values to observableList

    double endScanTime = System.currentTimeMillis();
    String updateStartupTaskText = "System Scan finished in "+ ((endScanTime-startScanTime)/1000) + " Seconds";
    startUpProgress = 1;
    UpdateTask(taskID, updateStartupTaskText, startUpProgress);}

AddTask()

public static String AddTask(String taskText, Double progress) {
    //TODO: do Add Task Logic here
    long longTaskId = System.currentTimeMillis(); //Define TaskID
    String taskId = ""+longTaskId;

    Label lblTaskEntry = new Label();
    lblTaskEntry.setText(taskText);
    lblTaskEntry.getStyleClass().add("TaskEntryLabel");
    lblTaskEntry.setId(taskId + "Label");

    Separator separatorTasks = new Separator();
    separatorTasks.setPrefWidth(230);

    ProgressIndicator piTaskEntry = new ProgressIndicator();
    piTaskEntry.getStyleClass().add("TaskEntryProgressIndicator");
    piTaskEntry.setId(taskId + "ProgressIndicator");
    if (progress != 0){
        piTaskEntry.setProgress(progress);
        piTaskEntry.setMaxSize(55, 55); // set maxsize of Progress Indicator if he has progress > 0 and Determinate
    }else {
        piTaskEntry.setMaxSize(35,35);//set maxsize for Progress Indicator if he has state of Indeterminate
    }

    HBox hBoxProgressPaneInnerTaskEntry = new HBox();
    hBoxProgressPaneInnerTaskEntry.setAlignment(Pos.CENTER_LEFT);
    hBoxProgressPaneInnerTaskEntry.setId(taskId + "Hbox");
    hBoxProgressPaneInnerTaskEntry.getChildren().addAll(piTaskEntry, lblTaskEntry);
    vBoxProgressPaneInner.getChildren().add(0, separatorTasks);
    vBoxProgressPaneInner.getChildren().add(1, hBoxProgressPaneInnerTaskEntry);
    return taskId;
}

1 个答案:

答案 0 :(得分:1)

你似乎在FX应用程序线程上做了这一切。该线程负责更新UI,因此在整个ScanSystem()(原文如此)方法完成之前,不会对UI进行实际更改。

要解决此问题,您需要在后台线程中执行长时间运行的操作,并使用Platform.runLater(...)在FX应用程序线程上安排对UI的增量更新:

private void scanSystem() {
    new Thread(() -> {
        double startScanTime = System.currentTimeMillis();
        //Preparing Event
        String startupTaskText = "System Scan has started!";
        double startUpProgress = 0;
        String taskID = App.addTask(startupTaskText, startUpProgress);

        String [][] snapshotArray = manager.getSystemSnapshotsScanSystem(); //Get snapshots from Manager in Array
        tableScanSystemForSnapshots.setItems(getSnapshot(snapshotArray)); //push values to observableList

        double endScanTime = System.currentTimeMillis();
        String updateStartupTaskText = "System Scan finished in "+ ((endScanTime-startScanTime)/1000) + " Seconds";
        startUpProgress = 1;
        updateTask(taskID, updateStartupTaskText, startUpProgress);
    }).start();

}

public static String addTask(String taskText, Double progress) {
    //TODO: do Add Task Logic here
    long longTaskId = System.currentTimeMillis(); //Define TaskID
    String taskId = ""+longTaskId;

    Platform.runLater(() -> {

        Label lblTaskEntry = new Label();
        lblTaskEntry.setText(taskText);
        lblTaskEntry.getStyleClass().add("TaskEntryLabel");
        lblTaskEntry.setId(taskId + "Label");

        Separator separatorTasks = new Separator();
        separatorTasks.setPrefWidth(230);

        ProgressIndicator piTaskEntry = new ProgressIndicator();
        piTaskEntry.getStyleClass().add("TaskEntryProgressIndicator");
        piTaskEntry.setId(taskId + "ProgressIndicator");
        if (progress != 0){
            piTaskEntry.setProgress(progress);
            piTaskEntry.setMaxSize(55, 55); // set maxsize of Progress Indicator if he has progress > 0 and Determinate
        }else {
            piTaskEntry.setMaxSize(35,35);//set maxsize for Progress Indicator if he has state of Indeterminate
        }

        HBox hBoxProgressPaneInnerTaskEntry = new HBox();
        hBoxProgressPaneInnerTaskEntry.setAlignment(Pos.CENTER_LEFT);
        hBoxProgressPaneInnerTaskEntry.setId(taskId + "Hbox");
        hBoxProgressPaneInnerTaskEntry.getChildren().addAll(piTaskEntry, lblTaskEntry);
        vBoxProgressPaneInner.getChildren().add(0, separatorTasks);
        vBoxProgressPaneInner.getChildren().add(1, hBoxProgressPaneInnerTaskEntry);
    });

    return taskId;
}