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
- Git.
- Java 8.
- JxBrowser license key, or a free 30-day evaluation key.
Getting the project
Clone the GitHub repository using the following command:
git clone https://github.com/TeamDev-IP/JxBrowser-QuickStart-Gradle-Swing.git
git checkout v7
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:
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:
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 = "7.41.6"
}
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)
}
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:
- Initialize an engine (Chromium) instance.
- Create a browser instance.
- Load the required web page.
- Embed a web view component into a Swing frame to display the loaded web page.
import static com.teamdev.jxbrowser.engine.RenderingMode.HARDWARE_ACCELERATED;
import static javax.swing.SwingUtilities.invokeLater;
import com.teamdev.jxbrowser.browser.Browser;
import com.teamdev.jxbrowser.engine.Engine;
import com.teamdev.jxbrowser.view.swing.BrowserView;
import java.awt.Frame;
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.
Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
// Create a Browser instance.
Browser browser = engine.newBrowser();
invokeLater(() -> {
Frame 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.engine.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.newInstance(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
- Learn more how to add JxBrowser to a Gradle project.
- Read about how to embed JxBrowser into a Swing app.
- Discover all JxBrowser features by checking out our guides.