Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javafx: Close alert box (or, any dialog box) programatically

Tags:

dialog

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

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

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)

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.

Thanks in advance!

Edit: Including sample code-

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

    }

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.

like image 901
Dilpreet Kaur Avatar asked Jun 10 '15 11:06

Dilpreet Kaur


1 Answers

Try this

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

Or for more general

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 " );
    }
}
like image 163
Uluk Biy Avatar answered Sep 20 '22 23:09

Uluk Biy