我有一个JavaFx应用程序,我在其中显示一个警告框:
alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Title");
alert.setHeaderText("Some Text");
alert.setContentText("Choose your option.");
buttonTypeOne = new ButtonType("Yes");
buttonTypeCancel = new ButtonType("No", ButtonData.CANCEL_CLOSE);
alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeCancel);
Optional<ButtonType> result = alert.showAndWait();
Run Code Online (Sandbox Code Playgroud)
因为警报会执行showAndWait,所以它会显示,直到用户按下"是"或"否"或"关闭"对话框.
问题:我需要从其他地方以编程方式关闭此对话框.为了详细描述,假设用户不选择任何选项,持续20秒,或假设被证明对于一些后台进程刚刚拿到过这个警告框,现在我希望通过设置关闭这个提示框result是buttonTypeCancel,代替用户按下任何按钮.(就像disposeSwing中的方法一样)
我怎样才能做到这一点?我尝试了Event.fireevent(/sf/answers/1572151591/),但我无法编写正确的事件关联.
提前致谢!
编辑:包括示例代码 -
Compute.java - 另一个执行计算的java类.
公共课Compute {警报警报;
public void function1{
Platform.runLater(new Runnable(){
public void run(){
alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Title");
alert.setHeaderText("Some Text");
alert.setContentText("Choose your option.");
buttonTypeOne = new ButtonType("Yes");
buttonTypeCancel = …Run Code Online (Sandbox Code Playgroud)我有以下代码来更新列单元格及其相应行的颜色:
calltypel.setCellFactory(column -> {
return new TableCell<CallLogs, String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? "" : getItem().toString());
setGraphic(null);
TableRow currentRow = getTableRow();
//This doesn't work
if(item.equals("a")){
item.setTextFill(Color.RED);
currentRow.setTextFill(Color.PINK);
}
else{
item.setTextFill(Color.GREEN);
currentRow.setTextFill(Color.BLUE);
}
}
};
});
Run Code Online (Sandbox Code Playgroud)
'if'条件的代码段不起作用.我无法确定对象的正确引用,以及执行此操作的最佳方法是什么.
谢谢!