java - 带有警报对话框的表中的侦听器
问题描述
我有 2 个表,我想根据在第一个表中选择的行来填充第二个表。在第二个表中填充数据之前,我想要一个警报对话框,其中如果Yes
按下则将在第一个表中选择新行并填充数据,但如果No
按下则将在第一个表中保持选中旧行。我怎样才能做到这一点?我尝试如下,但停留在第二部分。
table1.getSelectionModel().selectedItemProperty().addListener((obs, oldSelection, newSelection) -> {
if (newSelection != null) {
Alert alert = new Alert(AlertType.CONFIRMATION, "Unsaved Data Will be Deleted. Continue" + " ?",
ButtonType.YES, ButtonType.NO);
alert.showAndWait();
if (alert.getResult() == ButtonType.YES) {
//populate table2
} else if (alert.getResult() == ButtonType.NO) {
// Cancel New Selection and keep the old one
Platform.runLater(new Runnable() {
@Override
public void run() {
//stuck here
// how to keep old one selected instead new
//table1.getSelectionModel().select(oldSelection); //falls in a infinte loop
//return;
}
});
}
}
});
解决方案
不确定这是否有助于防止无限循环,但在有人找到更优雅的解决方案之前尝试它没有害处:
table1.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Foo>() {
private boolean reverting = false;
@Override
public void changed(ObservableValue<? extends Foo> observable, Foo oldSelection, Foo newSelection) {
if (newSelection != null && !reverting) {
Alert alert = new Alert(AlertType.CONFIRMATION, "Unsaved Data Will be Deleted. Continue" + " ?",
ButtonType.YES, ButtonType.NO);
alert.showAndWait();
if (alert.getResult() == ButtonType.YES) {
//populate table2
}
else if (alert.getResult() == ButtonType.NO) {
reverting = true;
table1.getSelectionModel().select(oldSelection);
}
}
reverting = false;
}
});
推荐阅读
- amazon-web-services - 即使在 EC2 上增加卷和分区大小后,设备上也没有剩余空间
- supervisord - “通知发送”的命令在主管中不起作用
- file - C 一般 sscanf 返回
- python - 如何找到定义了两个点的矩形的面积和周长?
- node.js - TypeError:无法在字符串上创建属性“_id”......即使在使用 JSON.Parse 之后??(MongoDB /Node.js / Twitter API)
- java - Java - 当从另一个方法创建对象时,尝试从主方法中打印对象的 get 方法
- css - 为什么此文本在 li 元素之外运行?
- r - 使用回收的向量元素将 R 列表转换为数据框
- excel - Excel VBA,切片器 - 如何使用 vba 从 excel 切片器中的一千个项目中选择一项?
- c# - ThreadPool.QueueUserWorkItem 使 UI 挂起