2

javafx では、GUI プロセスは別のスレッドで実行されます。FX スレッドではなくインジケーターが FX 要素であるため、サービスとタスクを使用してバックグラウンド スレッドに進行状況インジケーターを配置することはできません。javafxで複数のGUIスレッドを作成することは可能ですか?

または、他の GUI 要素が読み込まれているときに進行状況インジケーターを回転させ続ける別の方法はありますか? 現在、ペインがロードされるまでローリングを開始し、スタックします。

@FXML
public void budgetShow(ActionEvent event) {
    progressIndicator = new ProgressIndicator(-1.0);
    rootPane.getChildren().add(progressIndicator);
    progressIndicator.setVisible(true);
    progressIndicator.toFront();

    threadBudgetShow().start();
}

public Service<Void> threadBudgetShow() {
Service<Void> service = new Service<Void>() {
    @Override
    protected Task<Void> createTask() {
        return new Task<Void>() {
            @Override
            protected Void call() throws Exception {

                // Background Thread operations.                    
                final CountDownLatch latch = new CountDownLatch(1);

                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            // FX Thread opeartions.
                            // budgetAnchorPane - reload.
                            if (budgetAnchorPane == null || !budgetAnchorPane.isVisible()) {
                                budgetAnchorPane = new BudgetAnchorPane();
                                rootPane.getChildren().add(budgetAnchorPane);
                                budgetAnchorPane.setVisible(true);
                                budgetAnchorPane.getChildren().remove(budgetAnchorPane.budgetTypeComboBox);
                                budgetAnchorPane.budgetTypeComboBox = new BudgetTypeCombobox();
                                budgetAnchorPane.getChildren().add(budgetAnchorPane.budgetTypeComboBox);
                            }
                        } finally {
                            rootPane.getChildren().remove(progressIndicator);
                            latch.countDown();
                        }
                    }
                });
                latch.await();
                // Other background Thread operations.
                return null;
            }
        };
    }
};
return service;
}
4

1 に答える 1

4

不確定な進捗インジケータ

進行状況インジケーターが回転し続ける

これは、不確定な進行状況インジケーターを意味すると思います。

進行状況インジケーターは、デフォルトでは不確定な状態で開始されます。進行状況を不確定に設定することで、いつでもインジケーターを不確定な状態に戻すことができます。

progressIndicator.setProgress(ProgressIndicator.INDETERMINATE);

不確定な進行状況インジケーターとタスク

デフォルトの進行状況は不確定であるため、タスクが完了するまでタスクの進行状況を更新しない場合、タスクの進行状況にバインドされた進行状況インジケーターは、タスクの実行中は不確定のままになります。

サンプル

このサンプルの進行状況インジケーターは、タスクが完了するまで進行状況が不確定であることを示す一連の回転するドットです。

進捗

import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class ProgressTracker extends Application {

    final int N_SECS = 10;

    @Override
    public void start(Stage stage) throws Exception {
        Task task = createTask();

        stage.setScene(
            new Scene(
                createLayout(
                    task
                )
            )
        );
        stage.show();

        new Thread(task).start();
    }

    private Task<Void> createTask() {
        return new Task<Void>() {
            @Override public Void call() {
                for (int i=0; i < N_SECS; i++) {
                    if (isCancelled()) {
                        break;
                    }
                    // uncomment updateProgress call if you want to show progress
                    // rather than let progress remain indeterminate.
                    // updateProgress(i, N_SECS);
                    updateMessage((N_SECS - i) + "");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        return null;
                    }
                }

                updateMessage(0 + "");
                updateProgress(N_SECS, N_SECS);

                return null;
            }
        };
    }

    private HBox createLayout(Task task) {
        HBox layout = new HBox(10);

        layout.getChildren().setAll(
            createProgressIndicator(task),
            createCounter(task)
        );

        layout.setAlignment(Pos.CENTER_RIGHT);
        layout.setPadding(new Insets(10));

        return layout;
    }

    private ProgressIndicator createProgressIndicator(Task task) {
        ProgressIndicator progress = new ProgressIndicator();

        progress.progressProperty().bind(task.progressProperty());

        return progress;
    }

    private Label createCounter(Task task) {
        Label counter = new Label();

        counter.setMinWidth(20);
        counter.setAlignment(Pos.CENTER_RIGHT);
        counter.textProperty().bind(task.messageProperty());
        counter.setStyle("-fx-border-color: forestgreen;");

        return counter;
    }

    public static void main(String[] args) {
        launch(args);
    }
}
于 2013-11-12T06:13:02.570 に答える