Over a million developers have joined DZone.

Web Browser in Your Java Swing Application

DZone's Guide to

Web Browser in Your Java Swing Application

· Java Zone
Free Resource

Learn how to stop testing everything every sprint and only test the code you’ve changed. Brought to you by Parasoft.

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.


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

Get the top tips for Java developers and best practices to overcome common challenges. Brought to you by Parasoft.


Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}