我目前正在研究我的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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
另外,我的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();
}
}
Run Code Online (Sandbox Code Playgroud)
我想点击"删除"按钮后,警告框出现,请求许可,然后执行其余的删除代码.
我还考虑添加Alert而不是我的AlertBox类,fe:http://code.makery.ch/blog/javafx-dialogs-official/ (Confirmation Dialog)但我不知道如何实现它.
任何帮助都会很棒!谢谢 :)
我将从您提到的网站借用代码.
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);
}
}
});
Run Code Online (Sandbox Code Playgroud)