我目前正在处理我的 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 类,f.e: 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);
}
}
});
关于单击按钮时的 JavaFX 警告框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44101426/