Over a million developers have joined DZone.

HTML/CSS/JavaScript GUI in Java Swing Application

The following code demonstrates how simple the process of embedding web browser component into your Java Swing/AWT/JavaFX desktop application.

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

There are a lot of desktop applications that integrate web browser control to display HTML content and communicate with web services and web applications directly from app: iTunes, Adobe Brackets, Evernote, Amazon Music, Steam Client, etc. If you develop similar type of a desktop application using Java technology, and you need to embed safe, fast, lightweight web browser control that supports all modern web technologies such as HTML5, CSS, JavaScript, Flash, etc., then JxBrowser is what you need.

The following code demonstrates how simple the process of embedding web browser component into your Java Swing/AWT/JavaFX desktop application:


import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

import javax.swing.*;
import java.awt.*;

public class BrowserSample {
   public static void main(String[] args) {
       Browser browser = new Browser();
       BrowserView browserView = new BrowserView(browser);

       JFrame frame = new JFrame("JxBrowser");
       frame.add(browserView, BorderLayout.CENTER);
       frame.setSize(700, 500);



import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.javafx.BrowserView;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class JavaFXSample extends Application {
   public void start(Stage primaryStage) {
       Browser browser = new Browser();
       BrowserView browserView = new BrowserView(browser);

       StackPane pane = new StackPane();
       Scene scene = new Scene(pane, 700, 500);


   public static void main(String[] args) {

Note: take a look at Quick Start Guide to get more information about how to add JxBrowser library into your Java project using your favorite IDE.

HTML+CSS+JavaScript as a GUI

With JxBrowser your Java desktop application GUI can be built with HTML+CSS+JavaScript. It means that you can actually use any modern HTML5 UI toolkit to build modern, user-friendly interface of your Java desktop application. You don’t need to hire Swing/AWT developers. GUI of your Java app can be built by HTML+CSS+JavaScript developers. It significantly reduces the cost of Java project development.

The following simple application demonstrates how to create New Account Dialog built with HTML+CSS+JavaScript into your Java Swing/AWT desktop application.

First we create HTML document with New Account dialog content. In the following document we use one of the most popular Bootstrap HTML UI framework to build dialog’s UI:

<!DOCTYPE html>
   <meta charset="utf-8" />
   <title>Registration Form</title>
   <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
   <link href='http://fonts.googleapis.com/css?family=Roboto:300' rel='stylesheet' type='text/css'>
   <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
   <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
           font:12px/15px Roboto, "Helvetica Neue", Helvetica, sans-serif;
       .btn {
           font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
           margin:0 auto;
       .main-form {
           width: 360px;
           min-height: 360px;
           background: #fff;
           border-radius: 60px;
           margin:0px auto 20px;
           padding: 20px;
       .form-logo {
           font-size: 100px;
           color: #708090;
function newAccount() {
   var firstName = document.getElementById("firstname").value;
   var lastName = document.getElementById("lastname").value;
   var phone = document.getElementById("phone").value;
   var email = document.getElementById("email").value;
   // Call Java callback function and pass text fields values.
   onCreateAccount(firstName, lastName, phone, email);
<div id="wrapper">
   <div class="main-form">
       <form action="#" method="POST">
               <div class="text-center">
                   <span class="form-logo glyphicon glyphicon-user"></span>
               <div class="form-body">
                   <h1 class="form-title text-center">New Account</h1>
                   <div class="form-group">
                       <input class="form-control" type="text" id="firstname" name="firstname" placeholder="First Name">
                   <div class="form-group">
                       <input class="form-control" type="text" id="lastname" name="surname" placeholder="Last Name">
                   <div class="form-group">
                       <input class="form-control" type="text" id="phone" name="phone" placeholder="Phone">
                   <div class="form-group">
                       <input class="form-control" type="email" id="email" name="email" placeholder="Email">
                   <div class="form-group text-center">
                       <button class="btn btn-default btn-lg" type="button" onclick="newAccount();">New Account</button>

This dialog has First Name, Last Name, Phone, Email text fields, and New Account button. In Java application we need to display a dialog with this HTML content, let the user to fill all text fields and click New Account button. In Java code we need to be notified when user clicks the button, read text fields values to create a new account in our application. The following Java example demonstrates how we can do it with JxBrowser:

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.BrowserFunction;
import com.teamdev.jxbrowser.chromium.JSValue;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.concurrent.atomic.AtomicReference;

public class HTMLUISample {
   public static void main(String[] args) {
       final JFrame frame = new JFrame("HTMLUISample");
       final JButton newAccountButton = new JButton("New Account...");
       newAccountButton.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               Account account = createAccount(frame);
               // Displays created account's details
               JOptionPane.showMessageDialog(frame, "Created Account: " + account);

       JPanel contentPane = new JPanel();

       frame.add(contentPane, BorderLayout.CENTER);
       frame.setSize(300, 75);

   private static Account createAccount(JFrame parent) {
       final AtomicReference<Account> result = new AtomicReference<Account>();
       final JDialog dialog = new JDialog(parent, "New Account", true);

       // Create Browser instance.
       final Browser browser = new Browser();
       // Register Java callback function that will be invoked from JavaScript
       // when user clicks New Account button.
       browser.registerFunction("onCreateAccount", new BrowserFunction() {
           public JSValue invoke(JSValue... args) {
               // Read text field values received from JavaScript.
               String firstName = args[0].getString();
               String lastName = args[1].getString();
               String phone = args[2].getString();
               String email = args[3].getString();
               // Create a new Account instance.
               result.set(new Account(firstName, lastName, phone, email));
               // Close dialog.
               // Return undefined (void) to JavaScript.
               return JSValue.createUndefined();
       // Load HTML with dialog's HTML+CSS+JavaScript UI.

       dialog.addWindowListener(new WindowAdapter() {
           public void windowClosing(WindowEvent e) {
               // Dispose Browser instance because we don't need it anymore.
               // Close New Account dialog.
       // Embed Browser Swing component into the dialog.
       dialog.add(new BrowserView(browser), BorderLayout.CENTER);
       dialog.setSize(400, 500);

       return result.get();

   public static class Account {
       public final String firstName;
       public final String lastName;
       public final String phone;
       public final String email;

       public Account(String firstName, String lastName, String phone, String email) {
           this.firstName = firstName;
           this.lastName = lastName;
           this.phone = phone;
           this.email = email;

       public String toString() {
           return "Account{" +
                   "firstName='" + firstName + '\'' +
                   ", lastName='" + lastName + '\'' +
                   ", phone='" + phone + '\'' +
                   ", email='" + email + '\'' +

Now run this Java application and click New Account button:

Fill all text fields in the opened dialog and click New Account button:

Once user clicks New Account button, Java application is notified about click and reads new account information from the dialog:

Using this technique and JxBrowser library you can build and display any HTML+CSS+JavaScript UI in your Java desktop application.

Bring the power of Chromium engine into your Java desktop app!

Useful Links:

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}