Vladimir has posted 16 posts at DZone. View Full User Profile

Web browser in your Java Swing application

12.20.2010
| 36485 views |
  • submit to reddit

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

0
Published at DZone with permission of its author, Vladimir Ikryanov.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)