我有一个 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();
由于警报执行showAndWait
,因此它将一直显示,直到用户按下"is"或“否”或“关闭”对话框为止。
问题:我需要从其他地方以编程方式关闭此对话框。详细来说,如果,假设用户在 20 秒内没有选择任何选项,或者假设某个刚刚结束的后台进程显示了此警报框,现在我希望通过设置 result 来关闭此警报框
为 buttonTypeCancel
,而不是用户按下任何按钮。
(就像 Swing 中的 dispose
方法)
我该怎么做?我尝试了 Event.fireevent ( https://stackoverflow.com/a/22459308/3155986 ),但我无法编写关联的正确事件。
提前致谢!
编辑:包括示例代码-
- MainApp.java - 负责处理应用程序的 Java 类
- Controller.java - 相应的 Controller 文件
- Design.fxml - 应用程序的 FXML 文件,通过 MainApp.java 加载并由 Controller.java 控制
Compute.java - 另一个执行计算的 java 类。
公共(public)计算类{ 警报警报;
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 代码,但不知道如何实现。
最佳答案
试试这个
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;
}
}
}
<小时/>
完整示例:
@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:以编程方式关闭警报框(或任何对话框),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30755370/