【问题标题】:Disable the Cancel Button in Progress Bar in JavaFx在 JavaFx 中禁用进度条中的取消按钮
【发布时间】:2016-08-02 06:41:14
【问题描述】:

我在 javafx 中做了一个进度条。默认有一个取消Button。我只想在我的任务完成后禁用这个取消按钮。

jobProgressView.setGraphicFactory(task -> {
    return new Button("save");
});

【问题讨论】:

    标签: javafx java-8


    【解决方案1】:

    没有更多代码,我只能猜测。即使您添加的代码也不足以了解您的实现中的所有内容。

    因此,此解决方案假定您有一个正在运行的任务,并在进度条上显示它的进度。这里的Task封装在一个服务中,可以重启(也许你也需要这个?)。

    import javafx.application.Application;
    import javafx.beans.binding.Bindings;
    import javafx.concurrent.Service;
    import javafx.concurrent.Task;
    import javafx.concurrent.Worker;
    import javafx.event.ActionEvent;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.ProgressBar;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    
    public class CancelButtonDemo extends Application {
    
        Service<Integer> service = new MyService();
    
        @Override
        public void start(Stage primaryStage) {
    
            Button start = new Button();
            Button cancel = new Button();
            ProgressBar progress = new ProgressBar(0);
    
            start.setText("Run Task");
            start.setOnAction((ActionEvent event) -> {
                if (!(service.getState().equals(Worker.State.READY))) {
                    service.reset();
                }
                progress.progressProperty().bind(service.progressProperty());
                service.start();
            });
            start.disableProperty().bind(service.runningProperty());
    
            cancel.setText("Cancel Task");
            cancel.setOnAction((ActionEvent event) -> {
                service.cancel();
                progress.progressProperty().unbind();
                progress.setProgress(0);
            });
            cancel.disableProperty().bind(Bindings.not(service.runningProperty()));
    
            VBox root = new VBox(20);
            root.setAlignment(Pos.CENTER);
            root.getChildren().addAll(start, progress, cancel);
            Scene scene = new Scene(root, 300, 250);
            primaryStage.setTitle("Cancel Button Demo");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            launch(args);
        }
    
        class MyService extends Service<Integer> {
    
            @Override
            protected Task<Integer> createTask() {
                return new Task<Integer>() {
    
                    @Override
                    protected Integer call() throws Exception {
                        int iterations;
                        for (iterations = 0; iterations < 10000000; iterations++) {
                            if (isCancelled()) {
                                updateMessage("Cancelled");
                                break;
                            }
                            updateMessage("Iteration " + iterations);
                            updateProgress(iterations, 10000000);
                        }
                        return iterations;
                    }
                };
            }
        }
    }
    

    上面的应用程序如下所示:

    【讨论】:

    • 我已经更新了我的答案。
    • 你已经做了这些按钮。但我的取消按钮是默认的。所以禁用它有点困难。
    • @GauravAgrawal 我在您添加的代码中看不到它?最好的是,您添加一个minimal reproducible example
    猜你喜欢
    • 2013-07-26
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多