简体   繁体   English

单击按钮上的JavaFX警报框

[英]JavaFX alert box on button click

I am currently working on my JavaFX ZOO project and I have a problem. 我目前正在JavaFX ZOO项目上工作,但是有问题。 I am showing all of my records in a TableView and one of the columns contains a delete button. 我将所有记录显示在TableView中,其中一列包含一个删除按钮。 It all works perfectly, but I want to have an alert box showing up after clicking that delete button, just for safety. 一切正常,但是为了安全起见,我想在单击该删除按钮后显示一个警告框。

So my delete button class looks like this: 所以我的删除按钮类如下所示:

    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);
        }
    }
}

Also, my AlertBox class looks like this: 另外,我的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();

    }

}

I want to make this so after i click "Delete" button, the alert box shows up, ask for permission, and after that the rest of the deletion code is being performed. 我要这样做,因此在我单击“删除”按钮后,将显示警报框,征求许可,然后执行其余的删除代码。

I also considered to add Alert instead of my AlertBox class, fe: http://code.makery.ch/blog/javafx-dialogs-official/ (Confirmation Dialog) but I have no idea how to implement it. 我还考虑添加Alert而不是我的AlertBox类,例如: http ://code.makery.ch/blog/javafx-dialogs-official/(确认对话框),但是我不知道如何实现它。

Any help would be great! 任何帮助将是巨大的! Thanks :) 谢谢 :)

I will borrow code from the website you mentioned. 我将从您提到的网站上借用代码。

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