Javafx: programmatically close the warning window (or any dialog box)

I have a JavaFx application in which I display a warning window 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();

Since the alert is running showAndWait, it will therefore be displayed until the user clicks the "Yes" or "No" or "Close" dialog box.

Problem: I need to close this dialog programmatically from another place. To clarify, if, say, the user did not select an option within 20 seconds or did not say that this warning window was shown for some background process that just ended, and now I want to close this notification window by setting resultin be buttonTypeCancel, instead of having the user press any button. (Like a method disposein Swing)

How can i do this? I tried Event.fireevent ( https://stackoverflow.com/a/1652395/ ), but I cannot record the corresponding event.

Thanks in advance!

Edit: Including sample code -

  • MainApp.java - Java class responsible for processing the application
  • Controller.java - Corresponding controller file
  • Design.fxml - FXML , MainApp.java Controller.java
  • 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.
    }
    

    }

, ? Compute.java javafx, , .

+4
3

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

close() .

, 5 , , .

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.stage.Stage;

import java.util.Optional;

public class Main extends Application{

    public static void main(String[] args) {
        launch(args);
    }

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

        Thread thread = new Thread(() -> {
            try {
                // Wait for 5 secs
                Thread.sleep(5000);
                if (alert.isShowing()) {
                    Platform.runLater(() -> alert.close());
                }
            } catch (Exception exp) {
                exp.printStackTrace();
            }
        });
        thread.setDaemon(true);
        thread.start();
        Optional<ButtonType> result = alert.showAndWait();
    }
}
+1

2 .

dialog = new Dialog<>();
dialog.setTitle("It a dialog!");
dialog.show();

    Thread newThread = new Thread(new Runnable() {
    @Override
        public void run() {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }

            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    dialog.close();
                }
                });
            }
    });
    newThread.start();
0

All Articles