JxBrowser in JavaFX
The easiest way to start working with JxBrowser in a JavaFX Maven 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-Maven-JavaFX.git
git checkout v7
cd JxBrowser-QuickStart-Maven-JavaFX
Run the JavaFX application
Use the following command to build and run JavaFX application:
mvn clean compile exec:java -Djxbrowser.license.key=<your_license_key>
Once launched, you will see a JavaFX application with a BrowserView
component displaying https://html5test.teamdev.com:
Project overview
This section explains how the Maven project is configured to include JxBrowser and
how a JxBrowser BrowserView
component is embedded into a JavaFX scene 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, fetch the Chromium binaries for all platforms, and add the JavaFX UI toolkit integration.
Here’s how the pom.xml
file is configured:
<?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>javafx</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<!-- Use the latest stable JxBrowser version. -->
<jxbrowser.version>7.41.6</jxbrowser.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<!-- Define the main class for the Java application. -->
<exec.mainClass>com.teamdev.jxbrowser.quickstart.maven.javafx.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 JavaFX UI toolkit integration. -->
<dependency>
<groupId>com.teamdev.jxbrowser</groupId>
<artifactId>jxbrowser-javafx</artifactId>
<version>${jxbrowser.version}</version>
</dependency>
</dependencies>
</project>
Embedding JxBrowser into JavaFX
In the JavaFX application source code you can see how to:
- Initialize an engine (Chromium) instance.
- Create a browser instance.
- Load the required web page.
- Embed a
BrowserView
component into a JavaFX scene to display the loaded web page.
import static com.teamdev.jxbrowser.engine.RenderingMode.HARDWARE_ACCELERATED;
import com.teamdev.jxbrowser.browser.Browser;
import com.teamdev.jxbrowser.engine.Engine;
import com.teamdev.jxbrowser.view.javafx.BrowserView;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public final class App extends Application {
public static void main(String[] args) {
launch(args); // Start the JavaFX application.
}
@Override
public void start(Stage primaryStage) {
// Initialize Chromium.
Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
Browser browser = engine.newBrowser();
// Load the required web page.
browser.navigation().loadUrl("https://html5test.teamdev.com");
// Create and embed JavaFX BrowserView component to display web content.
BrowserView view = BrowserView.newInstance(browser);
Scene scene = new Scene(new BorderPane(view), 1280, 800);
primaryStage.setTitle("JxBrowser JavaFX");
primaryStage.setScene(scene);
primaryStage.show();
// Shutdown Chromium and release allocated resources.
primaryStage.setOnCloseRequest(event -> engine.close());
}
}
What’s next
- Learn more how to add JxBrowser to a Maven project.
- Read about how to embed JxBrowser into a JavaFX app.
- Discover all JxBrowser features by checking out our guides.