addMouseWheelListener is not worked in JBCefBrowser component

已回答

I create a JBCefBrowser in my toolwindow panel with code like below:

webBrowser.loadURL(FILE_PREFIX + path);
JComponent displayComponent = webBrowser.getComponent();
displayComponent.addMouseWheelListener(event -> {
            if (event.isConsumed()) {
                return;
            }

            if (event.isControlDown()) {
                double zoomFactor = 0.1D;
                int rotation = event.getWheelRotation();
                double zoomLevel = webBrowser.getZoomLevel();
                if (rotation < 0) {
                    // zoom in
                    zoomLevel = Math.min(zoomLevel + zoomFactor, MAX_ZOOM_IN_SIZE);
                } else {
                    // zoom out
                    zoomLevel = Math.max(zoomLevel - zoomFactor, 0D);
                }

                webBrowser.setZoomLevel(zoomLevel);
                event.consume();
            }
        });
webPanel.add(displayComponent,
                GridUtil.getGridByRowAnchorAndFill(0, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH))

in IDEA 2020, it works. but failed in IDEA 2021 ,  the ‘rotation’ always be negative, even if the wheel is rotated down.

and in IDEA 2023 or later, it seems like the event can not be triggered.

0

Hi Tanwen886,
Starting version 2024.2 JBCefBrowser has getBrowserComponent method that returns the the proper component to subscribe for UI events.

Before that you can get the events target component this way, it should work for older platform versions as well:
 

JBCefBrowser b = ….; // get your browser
Component c = b.getCefBrowser().getUIComponent();
c.addMouseListener(…)


Another option is 

JBCefBrowser b = ….; // get your browser
Component c = ((JPanel)b.getComponent()).getComponent(0);
c.addMouseListener(…)
0

Thank you! I will have a try soon.

0

请先登录再写评论。