ca.weblite:webview · v1.0.7 · MIT

A native WebView, embedded in your Java Swing app.

Cross-platform — native WebKit / WebView2 rendering on macOS, Linux, and Windows. Native libs bundled in the jar — one Maven dependency, zero install steps.

  • macOS · WKWebView
  • Linux · WebKitGTK
  • Windows · WebView2
Step 1

Add the dependency

Drop this into your pom.xml. The jar bundles the native libraries for macOS, Linux, and Windows.

pom.xml
<dependency>
    <groupId>ca.weblite</groupId>
    <artifactId>webview</artifactId>
    <version>1.0.7</version>
</dependency>
Using Gradle?
build.gradle
implementation 'ca.weblite:webview:1.0.7'
Step 2

Show a web page in a JFrame

One factory call. WebViewComponent.create() returns the right native WebView for the current platform.

Demo.java
import ca.weblite.webview.swing.WebViewComponent;
import javax.swing.*;
import java.awt.*;

public class Demo {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            WebViewComponent wv = WebViewComponent.create();
            wv.setUrl("https://example.com");
            wv.setPreferredSize(new Dimension(900, 600));

            JFrame frame = new JFrame("WebView Demo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(wv, BorderLayout.CENTER);
            frame.pack();
            frame.setVisible(true);
        });
    }
}

That's it — mvn package && java -jar target/your-app.jar and you have a native WebView running inside Swing.

Platform support

WebViewComponent.create() returns a fully-functional native WebView on each supported platform — rendering, mouse, keyboard, scrolling, resize, and tab visibility all work out of the box.

macOS

Cocoa · WKWebView

Full native embedding on 10.13+. Hardware-accelerated WKWebView with Swing widgets layered around it.

Linux

WebKitGTK · X11

Full rendering, mouse, and keyboard via WebKitGTK. Requires libwebkit2gtk-4.1 (or 4.0) and an X11 display.

Windows

WebView2

Full native embedding on Windows 11 and recent Windows 10 via the Microsoft Edge WebView2 Runtime.

Talking to JavaScript

Three methods cover the whole interop surface — fire-and-forget, round-trip-with-result, and page-initiated callbacks.

eval(String js)

Fire-and-forget. Use for side effects.

wv.eval("document.title = 'Hi';");

evalAsync(String js)

Returns a CompletableFuture<String> resolved with the JSON-stringified result. Continuations land on the EDT.

wv.evalAsync("return [window.scrollX, window.scrollY];")
  .thenAccept(json -> System.out.println(json));

addJavascriptCallback(...)

Exposes a Java callback at window.<name>(arg) for the page to invoke.

wv.addJavascriptCallback("notify", arg -> {
    System.out.println("page said: " + arg);
});

Browser-initiated dialogs

window.alert, confirm, prompt, and <input type="file"> are all routed through WebViewDialogHandler. The default handler shows Swing dialogs; install your own to customise or suppress them.

Dialog handler
wv.setDialogHandler(new WebViewDialogHandler() {
    @Override public boolean confirmOpened(WebViewConfirmEvent e) {
        return JOptionPane.showConfirmDialog(
            frame, e.message(), "Confirm",
            JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION;
    }
});

Pass null to install the headless-test drop handler (alert no-op, confirm → false, prompt → null, file picker → empty list). Pass WebViewDialogHandler.DEFAULT to reset to the Swing default.

Runnable demos

Each demo has launcher scripts at the project root (run-mac-*.sh, run-linux-*.sh, run-windows-*.bat).

Building from source

shell
git clone https://github.com/webliteca/swingwebview
cd swingwebview
mvn -DskipTests package

Produces target/webview-1.0-SNAPSHOT.jar. Targets Java 8 bytecode; works on JDK 8 and any newer LTS. Native libs are rebuilt by build-mac.sh / build-linux.sh / build-windows.sh; the cross-platform fat jar is produced by the GitHub Actions release workflow.