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

前端 未结 3 863
误落风尘
误落风尘 2020-12-19 05:52

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

alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle(\"Title\");
alert.setHeaderText(         


        
相关标签:
3条回答
  • 2020-12-19 06:16

    You can invoke close() method on the Alert or any Dialog.

    Here is a simple example which waits for 5 secs, and if the Alert is still showing, it closes it.

    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();
        }
    }
    
    0 讨论(0)
  • 2020-12-19 06:17

    It's closing opened dialog in 2 second.

    dialog = new Dialog<>();
    dialog.setTitle("It's 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 讨论(0)
  • 2020-12-19 06:18

    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 " );
        }
    }
    
    0 讨论(0)
提交回复
热议问题