繁体   English   中英

单击按钮上的JavaFX警报框

[英]JavaFX alert box on button click

我目前正在JavaFX ZOO项目上工作,但是有问题。 我将所有记录显示在TableView中,其中一列包含一个删除按钮。 一切正常,但是为了安全起见,我想在单击该删除按钮后显示一个警告框。

所以我的删除按钮类如下所示:

    private class ButtonCell extends TableCell<Record, Boolean> {
    final Button cellButton = new Button("Delete");

    ButtonCell(){

        cellButton.setOnAction(new EventHandler<ActionEvent>(){

            @Override
            public void handle(ActionEvent t) {

                Animal currentAnimal = (Animal) ButtonCell.this.getTableView().getItems().get(ButtonCell.this.getIndex());

                data.remove(currentAnimal);
            }
        });
    }

    @Override
    protected void updateItem(Boolean t, boolean empty) {
        super.updateItem(t, empty);
        if(!empty){
            setGraphic(cellButton);
        }
    }
}

另外,我的AlertBox类如下所示:

public class AlertBox {

    public static void display(String title, String message){

        Stage window = new Stage();

        window.initModality(Modality.APPLICATION_MODAL);
        window.setTitle(title);
        window.setMinWidth(250);

        Label label = new Label();
        label.setText(message);
        Button deleteButton = new Button("I'm sure, delete!");

        VBox layout = new VBox(10);
        layout.getChildren().addAll(label,deleteButton);
        layout.setAlignment(Pos.CENTER);

        Scene scene = new Scene(layout);
        window.setScene(scene);
        window.showAndWait();

    }

}

我要这样做,因此在我单击“删除”按钮后,将显示警报框,征求许可,然后执行其余的删除代码。

我还考虑添加Alert而不是我的AlertBox类,例如: http ://code.makery.ch/blog/javafx-dialogs-official/(确认对话框),但是我不知道如何实现它。

任何帮助将是巨大的! 谢谢 :)

我将从您提到的网站上借用代码。

cellButton.setOnAction(new EventHandler<ActionEvent>(){

        @Override
        public void handle(ActionEvent t){

            Alert alert = new Alert(AlertType.CONFIRMATION);
            alert.setTitle("Confirmation Dialog");
            alert.setHeaderText("Look, a Confirmation Dialog");
            alert.setContentText("Are you ok with this?");

            Optional<ButtonType> result = alert.showAndWait();
            if (result.get() == ButtonType.OK){
                Animal currentAnimal = (Animal) ButtonCell.this.getTableView().getItems().get(ButtonCell.this.getIndex());
                data.remove(currentAnimal);
            }
        }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM