3

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;

            }
    });
adesh singh
  • 1,727
  • 9
  • 38
  • 70

1 Answers1

12

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");
Sergey Grinev
  • 34,078
  • 10
  • 128
  • 141
  • How would it be possible to retrieve the URL the webview 2 is supposed to open? I tried creating my own webview embedded in a jframe etc. and then returning it as something like myFrame.myJfxPane.getEngine(), but somehow the URL is not passed to the Engine – Myoch Aug 06 '17 at 17:48
  • @Myoch please ask it as a separate question and provide code which doesn't work for you. – Sergey Grinev Aug 06 '17 at 19:51