List icon Contents

JxBrowser in SWT

The easiest way to start working with JxBrowser in an SWT Maven project is to clone the GitHub repository where everything is already set up and ready to go.

Prerequisites

Getting the project

Clone the GitHub repository using the following command:

git clone https://github.com/TeamDev-IP/JxBrowser-QuickStart-Maven-SWT.git
cd JxBrowser-QuickStart-Maven-SWT

Run the SWT application

Use the following command to build and run SWT application:

mvn clean compile exec:java -Djxbrowser.license.key=<your_license_key>

Once launched, you will see an SWT application with a BrowserView component displaying https://html5test.teamdev.com:

BrowserView in SWT app

Project overview

This section explains how the Maven project is configured to include JxBrowser and how a JxBrowser BrowserView component is embedded into an SWT shell to display content of the loaded web page.

Configuring the Maven project

The Maven project is configured to use the JxBrowser Maven repository to fetch the necessary JxBrowser dependencies and fetch the Chromium binaries for all platforms.

Here’s how the pom.xml file is configured:

XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.teamdev.jxbrowser.quickstart.maven</groupId>
    <artifactId>swt</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <!-- Use the latest stable JxBrowser version. -->
       <jxbrowser.version>8.2.1</jxbrowser.version>
       
       <maven.compiler.source>17</maven.compiler.source>
       <maven.compiler.target>17</maven.compiler.target>
       
       <!-- Define the main class for the Java application. -->
       <exec.mainClass>com.teamdev.jxbrowser.quickstart.maven.swt.App</exec.mainClass>
    </properties>
    
    <repositories>
        <!-- Configures the Maven repository for JxBrowser. -->
        <repository>
            <id>com.teamdev</id>
            <url>https://europe-maven.pkg.dev/jxbrowser/releases</url>
        </repository>
    </repositories>

    <dependencies>
        <!-- Fetches Chromium binaries for all platforms. -->
        <dependency>
            <groupId>com.teamdev.jxbrowser</groupId>
            <artifactId>jxbrowser-cross-platform</artifactId>
            <version>${jxbrowser.version}</version>
            <type>pom</type>
        </dependency>

        <!-- Adds dependency to the SWT UI toolkit integration. -->
        <dependency>
            <groupId>com.teamdev.jxbrowser</groupId>
            <artifactId>jxbrowser-swt</artifactId>
            <version>${jxbrowser.version}</version>
        </dependency>
    </dependencies>

</project>

Embedding JxBrowser into SWT

In the SWT application source code you can see how to:

  1. Initialize an engine (Chromium) instance.
  2. Create a browser instance.
  3. Load the required web page.
  4. Embed a BrowserView component into an SWT shell to display the loaded web page.
Java
import static com.teamdev.jxbrowser.engine.RenderingMode.HARDWARE_ACCELERATED;

import com.teamdev.jxbrowser.engine.Engine;
import com.teamdev.jxbrowser.view.swt.BrowserView;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public final class App {

    public static void main(String[] args) {
        // Initialize Chromium.
        var engine = Engine.newInstance(HARDWARE_ACCELERATED);

        // Create a Browser instance.
        var browser = engine.newBrowser();

        // Load the required web page.
        browser.navigation().loadUrl("https://html5test.teamdev.com");

        var display = new Display();
        var shell = new Shell(display);
        shell.setText("JxBrowser SWT");
        shell.setLayout(new FillLayout());

        // Create and embed SWT BrowserView widget to display web content.
        var view = BrowserView.newInstance(shell, browser);
        view.setSize(1280, 800);

        shell.pack();
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        // Shutdown Chromium and release allocated resources.
        engine.close();

        display.dispose();
    }
}

What’s next