Java Embedded Browser

So why embed a browser in an application, when anybody can just access a site via Firefox, Google Chrome, Internet Explorer or others? Why re-invent the wheel?

In terms of security, this is useful because it can restrict access to a web host through the application or it can prevent users from accessing third-party plugins to exploit vulnerabilities with the website.

Another reason is to provide a unique user experience with custom controls and functionality without creating individual plugins for each browser.

Although creating a browser application can lead to poor distribution, this is not important if the purpose is to develop a standalone application that requires access to other online facilities.

The following are code examples to use an embedded browser in a Java application using the following technologies:

  • JavaFX WebView
  • Standard Widget Toolkit (SWT)
  • JxBrowser

Each has their advantages and disadvantages.
After running each example, visit HTML5 Test to see how well it handles HTML5 components.

JavaFX WebView

While WebView has been introduced in the recent years as a JavaFX 2 feature, it is still has a lot of trouble rendering some HTML5 features.

It does, however, have excellent support in accessing DOM elements and events.

I would recommend using this for testing purposes or for creating a standalone web application.

public class WebViewExample extends Application {
public static void main(String[] args) {
public void start(Stage stage) throws Exception {
stage.setTitle("JavaFX: WebView");
WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
String url = "";
StackPane sp = new StackPane();
Scene root = new Scene(sp);

Standard Widget Toolkit (SWT)

SWT has been around before JavaFX 1.0 and has very good compatibility across multiple platforms.

This is achieved by using the standard operating system containers and controls to construct the application, and makes for a clean and professional interface.

The other benefit is that the sites are rendered using the default installed browser engine, i.e. Windows = Internet Explorer, Mac OS X = Safari etc.

The main issue with SWT is that the browser is very limited in functionality except for display purposes.
It is difficult to query the DOM and capturing web element events must be performed through a series of JavaScript.

Since SWT is not part of the standard JDK, the following dependencies are required.

If you are using Eclipse then the libraries need to be added to the classpath:

1. Right click on the Java Project and click Properties

2. Select Java Build Path > Libraries > Add Variable…

3. Click the Configure Variables… button.

4. Click New to display the New Variable Entry dialog.

5. Enter the Name SWT_LIB and click File and browse to SWT library file.

NOTE: The SWT library is located in the Eclipse folder, e.g. C:/eclipse/plugins/org.eclipse.swt.win32.win32.x86_64_3.104.2.v20160212-1350.jar

6. Click OK out and go back to coding.

For Maven projects not using Eclipse, add the following to the POM.

For more details visit GitHub – maven-eclispe

<!-- To use the debug jar, add this -->

The following code creates a nice simple browser using SWT.

public final class SWTBrowserExample {
public static final void launch() {
Display display = new Display();
final Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
ToolBar toolbar = new ToolBar(shell, SWT.NONE);
ToolItem itemBack = new ToolItem(toolbar, SWT.PUSH);
ToolItem itemForward = new ToolItem(toolbar, SWT.PUSH);
ToolItem itemStop = new ToolItem(toolbar, SWT.PUSH);
ToolItem itemRefresh = new ToolItem(toolbar, SWT.PUSH);
ToolItem itemGo = new ToolItem(toolbar, SWT.PUSH);
GridData data = new GridData();
data.horizontalSpan = 3;
Label labelAddress = new Label(shell, SWT.NONE);
final Text location = new Text(shell, SWT.BORDER);
data = new GridData();
data.horizontalAlignment = GridData.FILL;
data.horizontalSpan = 2;
data.grabExcessHorizontalSpace = true;
final Browser browser = new Browser(shell, SWT.NONE);
data = new GridData();
data.horizontalAlignment = GridData.FILL;
data.verticalAlignment = GridData.FILL;
data.horizontalSpan = 3;
data.grabExcessHorizontalSpace = true;
data.grabExcessVerticalSpace = true;
final Label status = new Label(shell, SWT.NONE);
data = new GridData(GridData.FILL_HORIZONTAL);
data.horizontalSpan = 2;
final ProgressBar progressBar = new ProgressBar(shell, SWT.NONE);
data = new GridData();
data.horizontalAlignment = GridData.END;
/* event handling */
Listener listener = new Listener() {
public void handleEvent(Event event) {
ToolItem item = (ToolItem) event.widget;
String string = item.getText();
if (string.equals("Back"))
else if (string.equals("Forward"))
else if (string.equals("Stop"))
else if (string.equals("Refresh"))
else if (string.equals("Go"))
browser.addProgressListener(new ProgressListener() {
public void changed(ProgressEvent event) {
if ( == 0)
int ratio = event.current * 100 /;
public void completed(ProgressEvent event) {
browser.addStatusTextListener(new StatusTextListener() {
public void changed(StatusTextEvent event) {
browser.addLocationListener(new LocationListener() {
public void changed(LocationEvent event) {
if ( {
public void changing(LocationEvent event) {
itemBack.addListener(SWT.Selection, listener);
itemForward.addListener(SWT.Selection, listener);
itemStop.addListener(SWT.Selection, listener);
itemRefresh.addListener(SWT.Selection, listener);
itemGo.addListener(SWT.Selection, listener);
location.addListener(SWT.DefaultSelection, new Listener() {
public void handleEvent(Event e) {
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
public static void main(String[] args) {


JxBrowser is Chromium-based and is developed by TeamDev.

It is a very mature embedded browser that can be used in Java Swing or JavaFX 2.

The browser can support most of the HTML5 features, CSS3 and third-party plugins, such as Flash and Silverlight.

The only downside is that it is not free and it is quite expensive for the casual developer.

The pricing is as follows (as of 2016-03-31):

Per Developer USD 1,799
Project USD 5,299
Source Code USD 11,499
Company Wide USD 14,599

Refer to the JxBrowser – Licensing and Pricing section for more details.

However, you get what you pay for, and in addition to the JxBrowser library you get:

  • An API that is clean and easy to use
  • Multitude of functionality such as Audio & Video,
  • Printing, Zoom, Spell Checking and more.
  • Documentation that is organised, concise and with examples.
  • Support that has fast turnaround time and actually respond to feature requests and issues.
public class JxBrowserExample extends Application {
public void init() throws Exception {
// On Mac OS X Chromium engine must be initialized in non-UI thread.
if (Environment.isMac()) {
public void start(Stage primaryStage) {
Browser browser = new Browser();
BrowserView browserView = new BrowserView(browser);
StackPane pane = new StackPane();
Scene scene = new Scene(pane, 800, 600);
primaryStage.setTitle("JxBrowser: JavaFX");
public static void main(String[] args) {


JavaFX – Software platform for creating and delivering desktop applications.
Standard Widget Toolkit – Graphical widget toolkit for use with the Java platform.
JxBrowser – Embed a Chromium-based Swing/JavaFX component into Java application
GitHub – maven-eclipse – Maven for Eclipse





One response to “Java Embedded Browser”

  1. Will Iverson Avatar

    One more option I just found while doing research:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.