List icon Contents

JxBrowser in Swing

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

Run the Swing application

Use the following command to build and run Swing application:

./gradlew run -Djxbrowser.license.key=<your_license_key>

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

BrowserView in Swing app

Project overview

This section explains how the Gradle project is configured to include JxBrowser and how a JxBrowser BrowserView component is embedded into a Swing frame 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 and fetch the Chromium binaries for the current platform.

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

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

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

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

repositories {
    mavenCentral()
}

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

    // Adds dependency to the Swing UI toolkit integration.
    implementation(jxbrowser.swing)

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

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

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

tasks.withType<JavaExec> {
    // 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 })
    jvmArgs(
        // Grant access to the java.desktop/java.awt module to let
        // JxBrowser access the Swing focus traversal functionality.
        "--add-opens=java.desktop/java.awt=ALL-UNNAMED"
    )
}

Embedding JxBrowser into Swing

There are two Swing 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 web view component into a Swing frame to display the loaded web page.
Java
Kotlin
import static com.teamdev.jxbrowser.engine.RenderingMode.HARDWARE_ACCELERATED;
import static javax.swing.SwingUtilities.invokeLater;

import com.teamdev.jxbrowser.engine.Engine;
import com.teamdev.jxbrowser.view.swing.BrowserView;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;

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();

        invokeLater(() -> {
            var frame = new JFrame("JxBrowser Swing");
            frame.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    // Shutdown Chromium and release allocated resources.
                    engine.close();
                }
            });
            // Create and embed Swing BrowserView component to display web content.
            frame.add(BrowserView.newInstance(browser));
            frame.setSize(1280, 800);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

            // Load the required web page.
            browser.navigation().loadUrl("https://html5test.teamdev.com/");
        });
    }
}
import com.teamdev.jxbrowser.dsl.Engine
import com.teamdev.jxbrowser.engine.RenderingMode.HARDWARE_ACCELERATED
import com.teamdev.jxbrowser.view.swing.BrowserView
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import javax.swing.JFrame
import javax.swing.SwingUtilities

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

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

    SwingUtilities.invokeLater {
        JFrame("JxBrowser Swing").apply {
            // Shutdown Chromium and release allocated resources when the frame closes.
            addWindowListener(object : WindowAdapter() {
                override fun windowClosing(e: WindowEvent) {
                    engine.close()
                }
            })
            // Create and embed Swing BrowserView component to display web content.
            add(BrowserView.newInstance(browser))
            setSize(1280, 800)
            setLocationRelativeTo(null)
            isVisible = true
        }

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

What’s next