Javafx:以编程方式关闭警报框(或任何对话框) [英] Javafx: Close alert box (or, any dialog box) programatically

查看:1812
本文介绍了Javafx:以编程方式关闭警报框(或任何对话框)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JavaFx应用程序,其中我使用以下方式显示警告框:

I have a JavaFx application in which I display an alert box using:

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();

因为警报执行 showAndWait ,所以它将显示,直到用户按是或否或关闭对话框。

Because the alert executesshowAndWait, so it will be displayed till the user either presses 'Yes' or 'No' or 'Close' the dialog.

问题:我需要以编程方式从其他位置关闭此对话框。详细说明,如果,假设用户没有选择任何选项20秒,或者让我们说这个警告框显示刚刚结束的一些后台进程,现在我希望通过设置<$ c $来关闭此警报框c>结果为 buttonTypeCancel ,而不是用户按任意按钮。
(与Swing中的 dispose 方法一样)

Problem: I need to close this dialog programatically from somewhere else. To elaborate, if, let's say the user does not chose any option for 20 sec, or let's say this alert box was shown for some background process which just got over, and now I wish to close this alert box by setting result to be buttonTypeCancel, instead of the user pressing any button. (Like dispose method in Swing)

我该怎么做?我试过Event.fireevent( https://stackoverflow.com/a/22459308/3155986 ),但我无法写相关的正确事件。

How can I do this? I tried Event.fireevent (https://stackoverflow.com/a/22459308/3155986) but I am not able to write the correct event associated.

提前致谢!

编辑:包含示例代码 -

Including sample code-


  1. MainApp.java - 负责处理应用程序的Java类

  2. Controller.java - 对应的控制器文件

  3. Design.fxml - 通过MainApp.java加载并由Controller.java控制的应用程序的FXML文件

  4. Compute.java - 另一个java用于执行计算的类。

  1. MainApp.java - Java class responsible for handling the application
  2. Controller.java - Corresponding controller file
  3. Design.fxml - FXML file for the application which is loaded via MainApp.java and controlled by Controller.java
  4. Compute.java - Another java class to perform computations.

公共类计算{
提醒警报;

public class Compute{ Alert alert;

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.
}

}

此外,还有其他解决办法吗?最初,我想保持 Compute.java 清除任何javafx代码,但无法弄清楚如何。

Also, is there any alternate solution to do this? Originally, I wanted to keep the Compute.java clean of any javafx code but couldn't figure out how.

推荐答案

试试这个

public void function2 {
    Button cancelButton = ( Button ) alert.getDialogPane().lookupButton( buttonTypeCancel );
    cancelButton.fire();
}

或更一般

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;
        }
    }
}






完整示例:


Full example:

@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 " );
    }
}

这篇关于Javafx:以编程方式关闭警报框(或任何对话框)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆