List icon Contents

JxBrowser in SWT

The easiest way to start working with JxBrowser in an SWT Gradle 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-Gradle-SWT.git
cd JxBrowser-QuickStart-Gradle-SWT

Run the SWT application

Use the following command to build and run SWT application:

./gradlew run -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 Gradle 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 Gradle project

The Gradle project uses the JxBrowser Gradle plugin to add the necessary JxBrowser dependencies, fetch the Chromium binaries for the current platform, and adds the platform-specific SWT dependencies.

Here’s how the build.gradle.kts file is configured:

Kotlin
import org.apache.tools.ant.taskdefs.condition.Os
import org.apache.tools.ant.taskdefs.condition.Os.FAMILY_MAC

plugins {
    java
    application
    kotlin("jvm") version "2.0.0"

    // Allows adding JxBrowser dependencies.
    id("com.teamdev.jxbrowser") version "1.2.1"
}

repositories {
    mavenCentral()
}

jxbrowser {
    // Use the latest stable JxBrowser version.
    version = "8.2.1"
}

dependencies {
    // Detects the current platform and adds the corresponding Chromium binaries.
    implementation(jxbrowser.currentPlatform)

    // Adds dependency to the SWT UI toolkit integration.
    implementation(jxbrowser.swt)

    // Adds dependency to the JxBrowser Kotlin DSL.
    implementation(jxbrowser.kotlin)

    // Adds the platform-specific SWT dependencies.
    implementation(Swt.toolkitDependency)
}

Swt.configurePlatformDependency(project)

application {
    // Define the main class for the application.
    mainClass.set("com.teamdev.jxbrowser.quickstart.gradle.swt.App")

    // Define the main class for the Kotlin application.
    // mainClass.set("com.teamdev.jxbrowser.quickstart.gradle.swt.AppKt")
}

tasks.withType<JavaExec> {
    if (Os.isFamily(FAMILY_MAC)) {
        jvmArgs(
            // For macOS to run SWT under Cocoa.
            "-XstartOnFirstThread"
        )
    }

    // Assign all Java system properties from the command line to
    // the JavaExec task to pass the JxBrowser license key.
    systemProperties(System.getProperties().mapKeys { it.key as String })
}

Embedding JxBrowser into SWT

There are two SWT app implementations in the project: Java and Kotlin.

Both implementations are similar and demonstrate 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
Kotlin
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();
    }
}
import com.teamdev.jxbrowser.dsl.Engine
import com.teamdev.jxbrowser.dsl.JxBrowserLicense
import com.teamdev.jxbrowser.engine.RenderingMode.HARDWARE_ACCELERATED
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

fun main() {
    // Initialize Chromium.
    val engine = Engine(HARDWARE_ACCELERATED)

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

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

    val display = Display()
    val shell = Shell(display)
    shell.text = "JxBrowser SWT"
    shell.layout = FillLayout()

    // Create and embed SWT BrowserView widget to display web content.
    val 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