webview not opening the popup window in javafx

2020-02-11 08:20发布

问题:

I am trying to open a webpage in webview using javafx . This webpage opens a new popup window when click on a hyperlink

How can i open the new popup , when trying to open the same web page in default web browser like chrome , IE they are opening the pop up.

For creating the pop up i am using the following code.

Group group= new Group();
Scene scene= new Scene(group);
fxpanel.setScene(scene);    
WebView webview = new WebView ();
group.getChildren().add(webview);
it().getScreenSize().height);  
webview.setVisible(true);
webview.getEngine().setOnAlert(null);

eng= webview.getEngine();
eng.setJavaScriptEnabled(true);




try{
   String url ="http://www.lawcrux.com/mkwebchatblog/loginp.aspx";


         eng.load(url);

         eng.setCreatePopupHandler(
        new Callback<PopupFeatures, WebEngine>() {
            @Override
            public WebEngine call(PopupFeatures config) {

                JOptionPane.showMessageDialog(null,"clicked");
                return eng;

            }
    });

回答1:

You need to create WebView popup window yourself and provide WebEngine from callback. If you need new window, create new Stage with that WebView (not Swing one, JOptionPane can't store JavaFX WebView).

See next example:

    WebView wv = new WebView();
    wv.getEngine().setCreatePopupHandler(new Callback<PopupFeatures, WebEngine>() {

        @Override
        public WebEngine call(PopupFeatures p) {
            Stage stage = new Stage(StageStyle.UTILITY);
            WebView wv2 = new WebView();
            stage.setScene(new Scene(wv2));
            stage.show();
            return wv2.getEngine();
        }
    });


    StackPane root = new StackPane();
    root.getChildren().add(wv);

    Scene scene = new Scene(root, 300, 250);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
    wv.getEngine().load("http://www.i-am-bored.com/pop_up_blocker_test.html");