1

I am currently working on my JavaFX ZOO project and I have a problem. I am showing all of my records in a TableView and one of the columns contains a delete button. 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:

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, f.e: http://code.makery.ch/blog/javafx-dialogs-official/ (Confirmation Dialog) but I have no idea how to implement it.

Any help would be great! Thanks :)

1 Answer 1

7

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);
            }
        }
    });
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, great job! Thank You Chandler :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.