Skip to content

JavaFX WebView in Java Swing Project

i try to use the WebView in my java project, in my code is:

JFXPanel fxPanel = new JFXPanel();
fxPanel.setBounds(10, 48, 439, 362);

WebView webView = new WebView();
fxPanel.setScene(new Scene(webView));

but the this thown a exception

java.lang.IllegalStateException: Not on FX application thread; currentThread = main

And yes, this is not a JavaFx application.


You can embed JavaFX content into a Swing application using JFXPanel. Note that for this to work correctly, you have to be careful to create and access the Swing content on the AWT event dispatch thread, and create and access the JavaFX content on the FX Application Thread, so you will need to carefully manage code using SwingUtilities.invokeLater(...) and Platform.runLater(...). (See the documentation for more details.)

Creating a JFXPanel starts the FX Application toolkit, if it is not already started.

Here is a simple example of embedding a JavaFX WebView into a Swing application:

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.web.WebView;

public class FXWebViewInSwing {

    private JFXPanel jfxPanel ;

    public void createAndShowWindow() {
        JFrame frame = new JFrame();
        JButton quit = new JButton("Quit");
        quit.addActionListener(event -> System.exit(0));
        jfxPanel = new JFXPanel();

        JPanel buttonPanel = new JPanel();

        frame.add(BorderLayout.CENTER, jfxPanel);
        frame.add(BorderLayout.SOUTH, buttonPanel);

        frame.setSize(800,  800);

    private void createJFXContent() {
        WebView webView = new WebView();
        Scene scene = new Scene(webView);

    public static void main(String[] args) {
        FXWebViewInSwing swingApp = new FXWebViewInSwing();

enter image description here