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

余生长醉 提交于 2019-12-01 11:05:05

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

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;
  }});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!