This guide describes how to create, use, and close Browser.

Please consider reading the Architecture guide to better understand how the JxBrowser architecture is designed, what main components it provides, and how it works.

Creating Browser

To create a new Browser instance please use the Browser.newInstance(EngineId) static method. For example:

Browser browser = Browser.newInstance(engine.getId());

This method performs the following actions:

  1. Creates a new Browser instance.
  2. Loads the about:blank web page in it and waits until the web page is loaded completely.

Closing Browser

The Browser instance is running in a separate native process that allocates memory and system resources to be released. So, when a Browser instance is no longer needed, it should be closed through the Browser.close() method to release all the allocated memory and system resources. For example:

Browser browser = Browser.newInstance(engineId);
...
browser.close();

An attempt to use an already closed Browser instance will lead to the IllegalStateException.

The Browser instance is closed automatically in the following cases:

  1. When its Engine is closed or unexpectedly crashed.
  2. When Browser is a pop-up that is closed from JavaScript using window.close().

To get notifications when the Browser instance is closed, please use the BrowserClosed event. For example:

browser.on(BrowserClosed.class, event -> {});

To check whether Browser is closed please use the isClosed() method:

boolean closed = browser.isClosed();

Browser Size

By default Browser size is empty. If you want to work with DOM or Find Text on a web page please configure the size.

To update the size of the Browser please use setSize(int width, int height). For example:

browser.setSize(800, 600);

This method notifies Chromium that the size of the Browser has been changed. Chromium will update the DOM layout of the loaded web page and repaint its content asynchronously. So, it might take some time for the web page to be repainted after the method returns.

User Agent

You can override the default User Agent string and configure Browser to use a custom string. For example:

browser.setUserAgent("<user-agent>");

To get the current user agent string please use:

String userAgent = browser.getUserAgent();
Go Top