【问题标题】:AlertType.CONFIRMATION throws IllegalArgumentException in JavaFXAlertType.CONFIRMATION 在 JavaFX 中抛出 IllegalArgumentException
【发布时间】:2018-03-31 00:25:13
【问题描述】:

每次用户尝试从 ListView 中删除项目时,我都会尝试添加确认类型的警报窗口。但是每当我这样做时,一旦我按下按钮,就会抛出一个 IllegalArgumentException 说,添加重复的孩子。代码如下:

@FXML
private void handleDeleteCaption() {
    Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
    alert.setTitle("Delete Caption");
    alert.setHeaderText("Are you sure you want to delete this caption?");
    alert.setContentText("All its contents will be lost. Continue?");

    alert.getButtonTypes().addAll(ButtonType.YES, ButtonType.CANCEL);

    Optional<ButtonType> result = alert.showAndWait();
    if(result.isPresent() && result.get() == ButtonType.YES) {
        captionsList.getItems().remove(selectedCaption);
    }
}

当我添加类型警告的警报时,也是同样的问题。它仅在我未指定警报类型时才有效,即当我将其声明为 AlertType.NONE 时。 我在这里的缺失点是什么?

【问题讨论】:

  • 您是否在此方法的第一行或单击确定时收到 IllegalArgumentException?
  • @thurst0n 我在调用 showAndWait() 方法的那一行得到它

标签: java javafx alert illegalargumentexception


【解决方案1】:

您可能会遇到此异常,因为按钮取消已存在于类型 CONFIRMATION 所以你可以做

alert.getButtonTypes().clear();

之前

alert.getButtonTypes().addAll(ButtonType.YES, ButtonType.CANCEL);

一种更好的方法(正如 Slaw 在 cmets 中提到的那样)是调用 setAll 而无需清除并重新添加

alert.getButtonTypes().setAll(ButtonType.YES, ButtonType.CANCEL)

【讨论】:

    猜你喜欢
    • 2020-07-03
    • 1970-01-01
    • 1970-01-01
    • 2014-09-18
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多