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.

To work with the global zoom that will apply to all web pages please use the ZoomService class.

To control zoom of a web page loaded in a Browser instance use the Zoom class.

Default Zoom Level

The default zoom level for all web pages is 100%. To change the default zoom level please use the ZoomService.setDefaultZoomLevel(ZoomLevel) method.

The following code sets the default zoom level for all web pages to 150%:

zoomService.setDefaultZoomLevel(ZoomLevel.P_150);

Controlling Zoom

You can zoom content of a web page loaded in Browser programmatically using the Zoom class or using touch gestures in the environments with touch screen.

Zoom level is configured for each host separately. If you set a zoom level for the http://www.a.com web page, it will not be changed for the http://www.b.com web page.

To change the zoom level you need to wait until the web page is loaded completely.

Zoom In

To perform zoom in on a currently loaded web page use the following method:

zoom.in();

Zoom Out

To perform zoom out on a currently loaded web page use the following method:

zoom.out();

Setting Zoom Level

The following code sets zoom level of the loaded web page to 200%:

zoom.setLevel(ZoomLevel.P_200);

Resetting Zoom

To reset zoom level to the default value use the following code:

zoom.reset();

Disabling Zoom

You can disable zoom for all web pages loaded in a Browser using the Zoom.disable() method. This method disables the zoom functionality and resets the zoom level to the default value. After that all attempts to change the zoom level programmatically via JxBrowser Zoom API and using touch gestures on a touch screen device will be ignored.

For example:

zoom.disable();

Zoom Events

To get notifications when the zoom level for a particular web page has been changed use the ZoomChanged event. For example:

zoomService.on(ZoomChanged.class, event -> {
    String host = event.getHost();
    ZoomLevel zoomLevel = event.getZoomLevel();
});

Pinch to Zoom

Currently JxBrowser does not support zoom via a pinch-to-zoom trackpad gesture on macOS.

Go Top