不确定的 ProgressBar 在对话框的一部分时不设置动画 (JavaFX 10)

Indeterminate ProgressBar does not animate when part of a Dialog (JavaFX 10)

ProgressBar 不确定时,它有一个来回移动的动画。当 ProgressBar 是正常 Stage 的一部分时,这工作正常,但当 Dialog 的一部分时,这不起作用。相反,它似乎只是坐在动画的开头。但是,ProgressBar 在设置为某个确定值时会正确更新。 注意:Java8.

中没有出现该问题

没有任何异常迹象。

这是一个 MCVE (GIF of it in action):

import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class App extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button detButton = new Button("Launch Determinate Task");
        detButton.setOnAction(ae -> {
            ae.consume();
            createDialog(primaryStage, true)
                    .showAndWait();
        });

        Button indetButton = new Button("Launch Indeterminate Task");
        indetButton.setOnAction(ae -> {
            ae.consume();
            createDialog(primaryStage, false)
                    .showAndWait();
        });

        HBox btnBox = new HBox(detButton, indetButton);
        btnBox.setSpacing(10);
        btnBox.setAlignment(Pos.CENTER);

        StackPane root = new StackPane(btnBox, createDummyProgressNode());

        Scene scene = new Scene(root, 500, 300);
        primaryStage.setScene(scene);
        primaryStage.setTitle("ProgressBar Issue");
        primaryStage.setResizable(false);
        primaryStage.show();
    }

    private Node createDummyProgressNode() {
        Label label = new Label("ProgressBar to show animation in Stage.");

        ProgressBar progressBar = new ProgressBar();
        progressBar.setMaxWidth(Double.MAX_VALUE);

        VBox box = new VBox(label, progressBar);
        box.setMaxHeight(VBox.USE_PREF_SIZE);
        box.setSpacing(3);
        box.setAlignment(Pos.CENTER_LEFT);
        box.setPadding(new Insets(5));

        StackPane.setAlignment(box, Pos.BOTTOM_CENTER);

        return box;
    }

    private Dialog<?> createDialog(Stage owner, boolean determinate) {
        Task<?> task = new BackgroundTask(determinate);

        Dialog<?> dialog = new Dialog<>();
        dialog.initOwner(owner);
        dialog.setTitle("Background Task - " 
                + (determinate ? "Determinate" : "Indeterminate"));

        dialog.getDialogPane().setPrefWidth(300);
        dialog.getDialogPane().setContent(createDialogContent(task));

        dialog.getDialogPane().getButtonTypes().add(ButtonType.OK);
        dialog.getDialogPane().lookupButton(ButtonType.OK)
                .disableProperty().bind(task.runningProperty());

        dialog.setOnShown(de -> {
            de.consume();
            executeTask(task);
        });

        return dialog;
    }

    private Node createDialogContent(Task<?> task) {
        Label label = new Label();
        label.textProperty().bind(task.messageProperty());

        ProgressBar progressBar = new ProgressBar();
        progressBar.setMaxWidth(Double.MAX_VALUE);
        progressBar.progressProperty().bind(task.progressProperty());

        VBox box = new VBox(label, progressBar);
        box.setSpacing(3);
        box.setAlignment(Pos.CENTER_LEFT);

        return box;
    }

    private void executeTask(Task<?> task) {
        Thread thread = new Thread(task, "background-thread");
        thread.setDaemon(true);
        thread.start();
    }

    private static class BackgroundTask extends Task<Void> {

        private final boolean determinate;

        private BackgroundTask(boolean determinate) {
            this.determinate = determinate;
        }

        @Override
        protected Void call() throws Exception {
            final int loops = 1_000;
            for (int i = 0; i <= loops; i++) {
                updateMessage("Running... " + i);
                Thread.sleep(1L);
                if (determinate) {
                    updateProgress(i, loops);
                }
            }
            updateMessage("Complete");
            updateProgress(loops, loops);
            return null;
        }

    }

}

我试过这段代码:

我所有的测试都是在 Windows 10 Home(版本 1803)计算机上进行的。

我 90% 确定这是某种类型的回归错误,但以防万一...

  1. 还有其他人有这个问题吗?还是我做错了什么,它在 Java 8 上运行只是侥幸?
  2. 如果这是一个错误,有人知道任何解决方法吗?

我尝试过更改 DialogModalityowner 之类的操作,但都没有效果。它似乎也只是 ProgressBar 因为 Dialog 仍然可以移动并且消息更新(即 UI 没有冻结)。

我还没有尝试过使用其他动画 Node,例如 ProgressIndicator

P.S。我在 Java 错误数据库中搜索了这个或类似的问题,但一无所获;我本可以很容易地错过一个,但是.

更新:我已经提交了我自己的错误报告

错误报告可在此处找到:JDK-8207837

  • 受影响的版本:910openjfx-11
  • 受影响的平台:通用(全部)。
  • 固定版本:openjfx-12.

原来 Dialog 不是问题的根源。问题是由于在 Scene 已经添加到 Stage 之后将 ProgressBar 添加到 Scene;这导致问题的原因是因为 JDK-8216377. See this comment 有更好的解释。这是演示问题的附加测试代码:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class IndeterminateProgressBar extends Application {

    @Override
    public void start(Stage stage) {
        StackPane root = new StackPane();

        Scene scene = new Scene(root, 300, 150);
        stage.setScene(scene);

        // If the progress bar is added to the root after the root is
        // added to the scene and the scene is added to the stage, it will
        // fail to be shown.
        ProgressBar progBar = new ProgressBar();
        progBar.setProgress(-1);
        root.getChildren().add(progBar);

        stage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

如果您将 stage.setScene(scene) 移动到 root.getChildren().add(progBar) 之后,ProgressBar 将会动画化。

根据此信息,我认为使用 Dialog 时的解决方法是不可能的。 Dialog 有一个用于显示的内部 Stage。当在 Dialog 上设置 DialogPane 时,它会导致创建 Scene,然后将其添加到此 Stage。不幸的是,Dialog 是用 DialogPane 初始化的,因此无论 Scene 是什么,都会在添加 ProgressBar 之前将其添加到 Stage

要避免此问题,请使用 JavaFX 8 或 OpenJFX 12+。修复程序可能(或将要)向后移植到 OpenJFX 11,但我不确定(如果您知道某种方式,请发表评论)。