Over a million developers have joined DZone.

Web Browser in Your Java Swing Application

· Java Zone

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

Do you develop a desktop Java Swing application and need a component that could display all modern web pages including Flash and Ajax? In this case JxBrowser library is what you need.

JxBrowser gives you a possibility to embed a rich web browser component into your Swing-based Java desktop applications. So web documents, inside your application will look exactly like in a web browser that they are designed for. JxBrowser supports integration with Internet Explorer and Mozilla (corresponding Firefox 3) under Windows, Apple Safari and Mozilla under Mac OS X, and Mozilla under Linux platforms.

This library provides a lot of features, but let's see how easy you can embed a Browser component into your Java Swing application and navigate on Google web page:

import com.teamdev.jxbrowser.Browser;import com.teamdev.jxbrowser.BrowserFactory;import com.teamdev.jxbrowser.BrowserType;import javax.swing.*;import java.awt.*;public class BrowserSample {    public static void main(String[] args) {        Browser browser = BrowserFactory.createBrowser(BrowserType.Mozilla);         JFrame frame = new JFrame();        frame.add(browser.getComponent(), BorderLayout.CENTER);        frame.setSize(700, 500);        frame.setLocationRelativeTo(null);        frame.setVisible(true);         browser.navigate("http://www.google.com");    }}

That's it!

If you configured your Java project with JxBrowser library as well, then you should see the following output when you run this example.

In the next article I will show you how to listen for the browser events such as start and completion of navigation, track page downloading progress and more.

Any comments or suggestions as for the other articles will be highly appreciated.

Links:

About library: http://www.teamdev.com/jxbrowser
Samples: https://sites.google.com/a/teamdev.com/jxbrowser-support/demo-and-samples

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
java

The best of DZone straight to your inbox.

SEE AN EXAMPLE
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.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}