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, fe: 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 :)
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);
}
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.