HTML/CSS/JavaScript GUI in Java Swing Application
Join the DZone community and get the full member experience.
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); frame.setVisible(true); browser.loadURL("http://www.google.com"); >>
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 < @Override public void start(Stage primaryStage) < Browser browser = new Browser(); BrowserView browserView = new BrowserView(browser); StackPane pane = new StackPane(); pane.getChildren().add(browserView); Scene scene = new Scene(pane, 700, 500); primaryStage.setScene(scene); primaryStage.show(); browser.loadURL("http://www.google.com"); >public static void main(String[] args) < launch(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:
body < font:12px/15px Roboto, "Helvetica Neue", Helvetica, sans-serif; >select, input, .btn < font-family: "Helvetica Neue",Helvetica,Arial,sans-serif; >#wrapper < margin:0 auto; >.main-form < width: 360px; min-height: 360px; background: #fff; border-radius: 60px; margin:0px auto 20px; padding: 20px; >.form-logo