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

JxBrowser displays the content of various types, e.g. images, video, PDF etc, but in most cases you will display the HTML content of a web page. This guide shows how to access the content of a web page, get the currently selected text, find some text on a web page, save the web page as a file or a set of files, etc.

Accessing HTML

To get a string that represents HTML of loaded web page use Browser.getHTML() method. You must call this method only when web page is loaded completely. Otherwise you might receive incomplete HTML or empty string. The following code demonstrates how to wait until web page is loaded completely and get its HTML:

browser.addLoadListener(new LoadAdapter() {
    @Override
    public void onFinishLoadingFrame(FinishLoadingEvent event) {
        if (event.isMainFrame()) {
            System.out.println("HTML = " + event.getBrowser().getHTML());
        }
    }
});

Accessing Selection

To get selected HTML on the loaded web page use the Browser.getSelectedHTML() method as described in the example below:

String html = browser.getSelectedHTML();

Finding Text

JxBrowser API provides functionality that allows on the currently loaded web page:

  • Finding specified text
  • Highlighting all matches
  • Selecting first match

To find specified text on the loaded web page use the Browser.findText() method. This method returns SearchResult instance that provides access to search results such as number of matches and index of selected match.

Browser performs search only through visible content on the loaded document. If some text presented on the web page isn’t visible due to CSS rules, Browser will not go through this content during search. Also, Browser doesn’t search text on the document with size 0x0, so make sure that Browser component is visible and its size isn’t empty.

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.SearchParams;
import com.teamdev.jxbrowser.chromium.SearchResult;
import com.teamdev.jxbrowser.chromium.events.FinishLoadingEvent;
import com.teamdev.jxbrowser.chromium.events.LoadAdapter;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

import javax.swing.*;
import java.awt.*;

/**
 * This sample demonstrates, how to find text on the loaded web page.
 */
public class FindTextSample {
    public static void main(String[] args) {
        final Browser browser = new Browser();
        BrowserView browserView = new BrowserView(browser);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browserView, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.addLoadListener(new LoadAdapter() {
            @Override
            public void onFinishLoadingFrame(FinishLoadingEvent event) {
                if (event.isMainFrame()) {
                    SearchParams request = new SearchParams("find me");
                    // Find text from the beginning of the loaded web page.
                    SearchResult result = browser.findText(request);
                    System.out.println(result.indexOfSelectedMatch() + "/" +
                            result.getNumberOfMatches());
                    // Find the same text again from the currently selected match.
                    result = browser.findText(request);
                    System.out.println(result.indexOfSelectedMatch() + "/" +
                            result.getNumberOfMatches());
                }
            }
        });
        browser.loadHTML("<html><body><p>Find me</p><p>Find me</p></body></html>");
    }
}

Canceling Search

To clear highlights on a web page (search results) and cancel search use Browser.stopFindingText(StopFindAction action) method.

Saving Web Page to File

The library allows you to save web pages as a file or set of files. You can use Browser.saveWebPage(String filePath, String dirPath, SavePageType saveType) method to save the current web page. Before saving make sure the page is loaded completely.

String filePath = "C:\\SavedPages\\index.html";
String dirPath = "C:\\SavedPages\\resources";
browser.saveWebPage(filePath, dirPath, SavePageType.COMPLETE_HTML);

Saving Web Page to PNG

To take an image of the currently loaded web page you need to perform the steps below:

  1. Create Browser instance.
  2. Set the required Browser view size.
  3. Load the required web page or HTML and wait until it is loaded completely.
  4. Get java.awt.Image of the loaded web page.

Functionality that allows capturing image of loaded web page is available for Browser instance in the LIGHTWEIGHT rendering mode only. When the HEAVYWEIGHT rendering mode is used, web page’s content is rendered via GPU directly onto a native window/surface embedded and displayed in Java Swing/JavaFX container.

The example below demonstrates how to perform all these steps:

Swing

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.BrowserPreferences;
import com.teamdev.jxbrowser.chromium.BrowserType;
import com.teamdev.jxbrowser.chromium.Callback;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;
import com.teamdev.jxbrowser.chromium.swing.internal.LightWeightWidget;
import com.teamdev.jxbrowser.chromium.swing.internal.events.LightWeightWidgetListener;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.RenderedImage;
import java.io.File;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * The sample demonstrates how to get screen shot of the web page
 * and save it as PNG image file.
 */
public class HTMLToImageSample {
    public static void main(String[] args) throws Exception {
        final int viewWidth = 1024;
        final int viewHeight = 20000;
        // Disables GPU process and changes maximum texture size
        // value from default 16384 to viewHeight. The maximum texture size value
        // indicates the maximum height of the canvas where Chromium
        // renders web page's content. If the web page's height
        // exceeds the maximum texture size, the part of outsize the
        // texture size will not be drawn and will be filled with
        // black color.
        String[] switches = {
                "--disable-gpu",
                "--max-texture-size=" + viewHeight
        };
        BrowserPreferences.setChromiumSwitches(switches);

        // #1 Create LIGHTWEIGHT Browser instance.
        Browser browser = new Browser(BrowserType.LIGHTWEIGHT);
        BrowserView view = new BrowserView(browser);

        // #2 Register LightWeightWidgetListener.onRepaint() to get
        // notifications about paint events. We expect that web page
        // will be completely rendered twice:
        // 1. When its size is updated to viewWidth x viewHeight.
        // 2. When HTML content is loaded and displayed.
        final CountDownLatch latch = new CountDownLatch(2);
        LightWeightWidget widget = (LightWeightWidget) view.getComponent(0);
        widget.addLightWeightWidgetListener(new LightWeightWidgetListener() {
            @Override
            public void onRepaint(Rectangle updatedRect, Dimension viewSize) {
                // Make sure that all view content has been repainted.
                if (viewSize.equals(updatedRect.getSize())) {
                    latch.countDown();
                }
            }
        });

        // #3 Set the required view size.
        browser.setSize(viewWidth, viewHeight);

        // #4 Load web page and wait until web page is loaded completely.
        Browser.invokeAndWaitFinishLoadingMainFrame(browser, new Callback<Browser>() {
            @Override
            public void invoke(Browser browser) {
                browser.loadURL("https://teamdev.com/jxbrowser");
            }
        });

        // #5 Wait until Chromium renders web page content.
        latch.await(45, TimeUnit.SECONDS);

        // #6 Save java.awt.Image of the loaded web page into a PNG file.
        ImageIO.write((RenderedImage) widget.getImage(), "PNG",
                new File("teamdev.com.png"));

        // #7 Dispose Browser instance.
        browser.dispose();
    }
} 

JavaFX

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.BrowserPreferences;
import com.teamdev.jxbrowser.chromium.BrowserType;
import com.teamdev.jxbrowser.chromium.Callback;
import com.teamdev.jxbrowser.chromium.internal.LightWeightWidgetListener;
import com.teamdev.jxbrowser.chromium.javafx.BrowserView;
import com.teamdev.jxbrowser.chromium.javafx.internal.LightWeightWidget;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.io.File;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javax.imageio.ImageIO;

/**
 * The sample demonstrates how to get screen shot of the web page
 * and save it as PNG image file.
 */
public class JavaFXHTMLToImageSample extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(final Stage primaryStage) throws Exception {
        final int viewWidth = 1024;
        final int viewHeight = 15000;
        // Disables GPU process and changes maximum texture size
        // value from default 16384 to viewHeight. The maximum texture size value
        // indicates the maximum height of the canvas where Chromium
        // renders web page's content. If the web page's height
        // exceeds the maximum texture size, the part of outsize the
        // texture size will not be drawn and will be filled with
        // black color.
        String[] switches = {
                "--disable-gpu",
                "--disable-gpu-compositing",
                "--enable-begin-frame-scheduling",
                "--max-texture-size=" + viewHeight
        };
        BrowserPreferences.setChromiumSwitches(switches);

        // #1 Create LIGHTWEIGHT Browser instance
        final Browser browser = new Browser(BrowserType.LIGHTWEIGHT);
        final BrowserView view = new BrowserView(browser);

        // #2 Get javafx.scene.image.Image of the loaded web page
        final LightWeightWidget widget = (LightWeightWidget) view.getChildren().get(0);

        // #3 Register LightWeightWidgetListener.onRepaint() to get
        // notifications about paint events. We expect that web page
        // will be completely rendered twice:
        // 1. When its size is updated to viewWidth x viewHeight.
        // 2. When HTML content is loaded and displayed.
        final CountDownLatch latch = new CountDownLatch(2);
        widget.addLightWeightWidgetListener(new LightWeightWidgetListener() {
            @Override
            public void onRepaint(Rectangle updatedRect, Dimension viewSize) {
                if (viewSize.equals(updatedRect.getSize())) {
                    latch.countDown();
                }
            }
        });

        // #4 Set the required view size
        browser.setSize(viewWidth, viewHeight);

        // #5 Load web page and wait until web page is loaded completely
        Browser.invokeAndWaitFinishLoadingMainFrame(browser, new Callback<Browser>() {
            @Override
            public void invoke(Browser browser) {
                browser.loadURL("https://teamdev.com/jxbrowser");
            }
        });

        // #6 Wait until Chromium renders web page content.
        latch.await(45, TimeUnit.SECONDS);

        final Image image = widget.getImage();

        // #7 Save the image into a PNG file
        ImageIO.write(SwingFXUtils.fromFXImage(image, null), "PNG", new File("teamdev.com.png"));

        // #8 Dispose Browser instance.
        browser.dispose();

        // #9 Close the application.
        Platform.exit();
    }
}

Calculating Page Size

If you need to take screenshot of the whole web page including scrollable hidden parts and you don’t know dimension of the web page, then you need to calculate it using the following approach:

JSValue documentHeight = browser.executeJavaScriptAndReturnValue(
        "Math.max(document.body.scrollHeight, " +
        "document.documentElement.scrollHeight, document.body.offsetHeight, " +
        "document.documentElement.offsetHeight, document.body.clientHeight, " +
        "document.documentElement.clientHeight);");
JSValue documentWidth = browser.executeJavaScriptAndReturnValue(
        "Math.max(document.body.scrollWidth, " +
        "document.documentElement.scrollWidth, document.body.offsetWidth, " +
        "document.documentElement.offsetWidth, document.body.clientWidth, " +
        "document.documentElement.clientWidth);");

final int scrollBarSize = 25;
int viewWidth = documentWidth.asNumber().getInteger() + scrollBarSize;
int viewHeight = documentHeight.asNumber().getInteger() + scrollBarSize;

In this code we use JavaScript and DOM API to get dimension of the loaded document.

WebStorage

HTML5 supports Web Storage API that allows browsers to store key/value pairs, in a much more better way than using cookies. Web Storage API provides two mechanisms of storing data locally:

  • window.localStorage stores data with no expiration date.
  • window.sessionStorage stores data for one session (data is lost when the browser tab is closed).

JxBrowser provides API that allows working with both Local and Session storages. See the following methods:

  • Browser.getLocalWebStorage() returns the local web storage instance.
  • Browser.getSessionWebStorage() returns the session web storage instance.

Local and Session storages implement same WebStorage interface that provides methods for working with storage.

The Browser.getLocalWebStorage() method returns null if JavaScript hasn’t accessed the localStorage variable. The reason is that there’s a lazy initialization of the local storage in Chromium engine. You need access the localStorage variable in JavaScript to initialize local storage. After that you can access it via JxBrowser Web Storage API. It’s a limitation in the current implementation. The following code snippet demonstrates how to workaround this limitation:

browser.addLoadListener(new LoadAdapter() {
    @Override
    public void onDocumentLoadedInMainFrame(LoadEvent event) {
        Browser browser = event.getBrowser();
        // Initialize WebStorage
        browser.executeJavaScript("localStorage");
        
        // Access WebStorage
        WebStorage webStorage = browser.getLocalWebStorage();
        // Read and display the 'myKey' storage value.
        String itemValue = webStorage.getItem("myKey");
    }
});