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 Contents

This guide shows how to work with Zoom API.

JxBrowser allows to zoom content of a web page or of all web pages, get notifications when the zoom level of a web page has been changed, override the default zoom level, etc.

Controlling Zoom

You can zoom content of a web page loaded using the Browser.setZoomLevel(double zoomLevel), Browser.zoomIn(), Browser.zoomOut(), Browser.zoomReset() methods. Please note that zoom level can be applied only for the completely loaded web page. You need to wait until the web page is loaded completely and only then you can zoom it:

browser.addLoadListener(new LoadAdapter() {
    @Override
    public void onFinishLoadingFrame(FinishLoadingEvent event) {
        if (event.isMainFrame()) {
            event.getBrowser().setZoomLevel(2.0);
        }
    }
});

Disabling Zoom

By default, zoom functionality is enabled in JxBrowser. If you would like to disable zoom for specific Browser instance and don’t let the end user to zoom the web page loaded in this Browser instance, then you can use the Browser.setZoomEnabled(false) method.

Zoom Events

ZoomService allows you to register ZoomListener for receiving the zoom change events. Every time when zoom level for a specified web page has been changed, the onZoomChanged() event will be fired:

// Listen to zoom changed events
ZoomService zoomService = browser.getContext().getZoomService();
zoomService.addZoomListener(new ZoomListener() {
    public void onZoomChanged(ZoomEvent event) {
        System.out.println("event.getURL() = " + event.getURL());
        System.out.println("event.getZoomLevel() = " + event.getZoomLevel());
    }
});