New version is available You are viewing the documentation for JxBrowser 6 which is not supported since December 2019. Go to the current documentation.
List icon 目录

This page describes how to work with pop-ups, display or suppress them.

Overview

Any web page can display pop-up windows using the window.open() JavaScript function. For example:

window.open("http://www.google.com", "_blank", "toolbar=yes, 
    scrollbars=yes, resizable=yes, top=500, left=500, width=400, height=400");

By default, pop-up windows are enabled and displayed in both Swing and JavaFX UI toolkits.

JxBrowser API provides the PopupHandler class to handle pop-up windows creation. To handle pop-up windows creation you must register your own implementation of PopupHandler depending on GUI toolkit you use in your Java application. To get more information about how to handle pop-ups in Swing and JavaFX toolkits take a look at the following chapters:

Handling Pop-ups Swing

To handle pop-up windows in your Swing application you have two options:

Register default Swing pop-up handler implementation:

browser.setPopupHandler(new com.teamdev.jxbrowser.chromium.swing.DefaultPopupHandler());

Register your own implementation where you decide how exactly you would like to display a new pop-up window (e.g. in JFrame, JWindow, application tab):

browser.setPopupHandler(new PopupHandler() {
    public PopupContainer handlePopup(PopupParams params) {
        return new PopupContainer() {
            public void insertBrowser(final Browser browser, final Rectangle initialBounds) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        BrowserView browserView = new BrowserView(browser);
                        browserView.setPreferredSize(initialBounds.getSize());

                        final JFrame frame = new JFrame("Popup");
                        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                        frame.add(browserView, BorderLayout.CENTER);
                        frame.pack();
                        frame.setLocation(initialBounds.getLocation());
                        frame.setVisible(true);
                        frame.addWindowListener(new WindowAdapter() {
                            @Override
                            public void windowClosing(WindowEvent e) {
                                browser.dispose();
                            }
                        });

                        browser.addDisposeListener(new DisposeListener<Browser>() {
                            public void onDisposed(DisposeEvent<Browser> event) {
                                frame.setVisible(false);
                            }
                        });
                    }
                });
            }
        };
    }
});

Handling Pop-ups JavaFX

To handle pop-up windows in your JavaFX application you have two options:

Register default JavaFX pop-up handler implementation:

browser.setPopupHandler(new com.teamdev.jxbrowser.chromium.javafx.DefaultPopupHandler());

Register your own implementation where you decide how exactly you would like to display a new pop-up window:

browser.setPopupHandler(new PopupHandler() {
    public PopupContainer handlePopup(PopupParams params) {
        return new PopupContainer() {
            public void insertBrowser(final Browser browser, final Rectangle initialBounds) {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        final Stage stage = new Stage();
                        StackPane root = new StackPane();
                        Scene scene = new Scene(root, 800, 600);
                        BrowserView browserView = new BrowserView(browser);
                        root.getChildren().add(browserView);
                        stage.setScene(scene);
                        stage.setTitle("Popup");
                        if (!initialBounds.isEmpty()) {
                            stage.setX(initialBounds.getLocation().getX());
                            stage.setY(initialBounds.getLocation().getY());
                            stage.setWidth(initialBounds.width);
                            stage.setHeight(initialBounds.height);
                        }

                        stage.setOnCloseRequest(new EventHandler<javafx.stage.WindowEvent>() {
                            public void handle(javafx.stage.WindowEvent event) {
                                browser.dispose();
                            }
                        });

                        browser.addDisposeListener(new DisposeListener<Browser>() {
                            public void onDisposed(DisposeEvent<Browser> event) {
                                Platform.runLater(new Runnable() {
                                    public void run() {
                                        stage.close();
                                    }
                                });
                            }
                        });
                        stage.show();
                    }
                });
            }
        };
    }
});

Disabling Popups

To disable pop-up windows you must create and register your own implementation of PopupHandler and return null from the handlePopup() method as shown below:

browser.setPopupHandler(new PopupHandler() {
    public PopupContainer handlePopup(PopupParams params) {
        return null;
    }
});
Go Top