How can I make JavaFX web browser displays alert and confirm message

前端 未结 2 1843
半阙折子戏
半阙折子戏 2021-01-15 13:05

My java web browser don\'t display alert(\"message\"); and confirm(\"message\"); either, I ussually use c# web Browser component and it\'s works perfectly but I\'m new at t

相关标签:
2条回答
  • 2021-01-15 13:21

    I found a pretty solution :)

    for alert:

     webView.getEngine().setOnAlert(new EventHandler<WebEvent<String>>(){
    
                @Override
                public void handle(WebEvent<String> arg0) {            
                   JOptionPane.showMessageDialog(null,  arg0.getData(),"Mensaje", JOptionPane.INFORMATION_MESSAGE);
                }
    
            });
    

    for confirm:

       webView.getEngine().setConfirmHandler(new Callback<String, Boolean>() {
       public Boolean call(String msg) {
    
         int result = JOptionPane.showConfirmDialog(null,msg, "Mensaje",JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
    
         boolean b = (result != 0);
         return !b;
      }});
    
    0 讨论(0)
  • 2021-01-15 13:25

    For alert, register an onAlert handler with the web engine. For confirm, register a confirmHandler with the web engine. See the "User interface callbacks" section of the WebEngine documentation.

    Here's a quick example:

    import java.util.Optional;
    
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.ButtonType;
    import javafx.scene.control.Dialog;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.web.WebEngine;
    import javafx.scene.web.WebView;
    import javafx.stage.Stage;
    
    public class WebViewWithAlertAndConfirm extends Application {
    
        private WebEngine engine;
    
        @Override
        public void start(Stage primaryStage) {
            WebView webView = new WebView();
            engine = webView.getEngine();
    
            engine.setOnAlert(event -> showAlert(event.getData()));
            engine.setConfirmHandler(message -> showConfirm(message));
    
            String content =   
                    "<html>"
                    + "<head>"
                    + "<script language='javascript'>"
                    + "function doConfirm() {"
                    + "    var accepted = confirm('Are you sure?');"
                    + "    if (accepted) {"
                    + "       document.getElementById('result').innerHTML = 'Accepted';"
                    + "    } else {"
                    + "       document.getElementById('result').innerHTML = 'Declined';"
                    + "    }"
                    + "}"
                    + "</script>"
                    + "<body>"
                    + "<div><button onclick='alert(\"This is an alert!\")'>Show alert</button>"
                    + "<button onclick='doConfirm()'>Confirm</button>"
                    + "</div>"
                    + "<div id='result'/>"
                    + "</body>"
                    + "</html>";
    
            engine.loadContent(content);
    
            primaryStage.setScene(new Scene(new BorderPane(webView)));
            primaryStage.show();
        }
    
        private void showAlert(String message) {
            Dialog<Void> alert = new Dialog<>();
            alert.getDialogPane().setContentText(message);
            alert.getDialogPane().getButtonTypes().add(ButtonType.OK);
            alert.showAndWait();
        }
    
        private boolean showConfirm(String message) {
            Dialog<ButtonType> confirm = new Dialog<>();
            confirm.getDialogPane().setContentText(message);
            confirm.getDialogPane().getButtonTypes().addAll(ButtonType.YES, ButtonType.NO);
            boolean result = confirm.showAndWait().filter(ButtonType.YES::equals).isPresent();
    
            // for debugging:
            System.out.println(result);
    
            return result ;
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    
    0 讨论(0)
提交回复
热议问题