Javafx:以编程方式关闭警报框(或任何对话框)

Dil*_*aur 6 dialog javafx

我有一个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秒,或假设被证明对于一些后台进程刚刚拿到过这个警告框,现在我希望通过设置关闭这个提示框resultbuttonTypeCancel,代替用户按下任何按钮.(就像disposeSwing中的方法一样)

我怎样才能做到这一点?我尝试了Event.fireevent(/sf/answers/1572151591/),但我无法编写正确的事件关联.

提前致谢!

编辑:包括示例代码 -

  1. MainApp.java - 负责处理应用程序的Java类
  2. Controller.java - 对应的控制器文件
  3. Design.fxml - 应用程序的FXML文件,通过MainApp.java加载并由Controller.java控制
  4. 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 = new ButtonType("No", ButtonData.CANCEL_CLOSE);
          alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeCancel);
    
          Optional<ButtonType> result = alert.showAndWait();
          if (result.get() == buttonTypeOne){
          // ... user chose "One"
          } else {
          // ... user chose CANCEL or closed the dialog
          }
    
          }
      });
    }
    
    public void function2{
     //......Does some long computation here 
     //.... If the computation finishes before the user chooses 'Yes' or 'No' on alert box
    //...Then close the alertbox here and execute the code corresponding to buttonTypeCancel
    
    
    //..I tried alert.close(); and alert.hide(); but doesn't work.
    }
    
    Run Code Online (Sandbox Code Playgroud)

    }

此外,有没有替代解决方案来做到这一点?最初,我想保持Compute.java任何javafx代码的清洁,但无法弄清楚如何.

Ulu*_*Biy 2

尝试这个

public void function2 {
    Button cancelButton = ( Button ) alert.getDialogPane().lookupButton( buttonTypeCancel );
    cancelButton.fire();
}
Run Code Online (Sandbox Code Playgroud)

或者更一般的

public void function2 {
    for ( ButtonType bt : alert.getDialogPane().getButtonTypes() )
    {
        if ( bt.getButtonData() == ButtonBar.ButtonData.CANCEL_CLOSE )
        {
            Button cancelButton = ( Button ) alert.getDialogPane().lookupButton( bt );
            cancelButton.fire();
            break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

完整示例:

@Override
public void start( final Stage primaryStage )
{
    Alert alert = new Alert( Alert.AlertType.CONFIRMATION );
    alert.setTitle( "Title" );
    alert.setHeaderText( "Some Text" );
    alert.setContentText( "Choose your option." );
    ButtonType buttonTypeOne = new ButtonType( "Yes" );
    alert.initModality( Modality.NONE );
    ButtonType buttonTypeCancel = new ButtonType( "No", ButtonBar.ButtonData.CANCEL_CLOSE );
    alert.getButtonTypes().setAll( buttonTypeOne, buttonTypeCancel );

    Button b = new Button( "close alert" );
    b.setOnAction(( ActionEvent event ) ->
    {
        for ( ButtonType bt : alert.getDialogPane().getButtonTypes() )
        {
            System.out.println( "bt = " + bt );
            if ( bt.getButtonData() == ButtonBar.ButtonData.CANCEL_CLOSE )
            {
                Button cancelButton = ( Button ) alert.getDialogPane().lookupButton( bt );
                cancelButton.fire();
                break;
            }
        }
    });

    final Scene scene = new Scene( new Group( b ), 400, 300 );
    primaryStage.setScene( scene );
    primaryStage.show();

    Optional<ButtonType> result = alert.showAndWait();
    if ( result.get() == buttonTypeOne )
    {
        System.out.println( "one " );
    }
    else if( result.get() == buttonTypeCancel )
    {
        System.out.println( "cancel " );
    }
}
Run Code Online (Sandbox Code Playgroud)