How to Embed IE in Java 

Run Java Apps from SysTray 

Launch Native Applications from Java

Text Interfaces in Java

The Vista RSS Platform 

Visual C/C++ and Visual Basic programmers know how easy it is to embed a Web browser within an application. They have to just drag and drop the Web browser component on to the application's frame in Visual Studio. Unfortunately, Java programmers do not enjoy such convenience. The JEditorPane in Java is quite limited and gives up when it comes to displaying pages rich in DHTML and JavaScript. But, now with JDIC (JDesktop Integration Components) it's time to bid such limitations goodbye. 

JDIC a the Java desktop project at www.java.net and focuses on using native and platform-specific applications and facilities. According to java.net, “JDIC provides Java applications with access to facilities provided by the native desktop such as the mailer, the browser and registered document viewing applications. Additionally it provides the mechanisms by which Java applications can integrate into the native desktop such as registering Java applications as document viewers on the desktop and creating installer packages.” 

This article will briefly talk about JDIC and go through a simple Java application called IEInJava that embeds IE in a JFrame. 

Direct Hit!
Applies to: Java developers
USP: Embedding a browser in applications becomes easy
Links: https://jdic.dev.java.net/

First looks 
Below is the code of a simple Java application that embeds native Web browser using JDIC.

import org.jdesktop.jdic.browser.*;
import java.awt.*;
import javax.swing.*;
import java.net.*;
public class IEInJava {
public static void main(String[] args) {
WebBrowser webBrowser = new WebBrowser();
webBrowser.setSize(700,500);
JFrame frame = new JFrame("IE in Java"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane(); 
contentPane.add(webBrowser, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
URL url = null;
try { 
url = new URL("http://www.pcquest.com"); 
}
catch (MalformedURLException e) {
System.out.println(e);

webBrowser.setURL(url);
}
}

The lines in the code that do that magic are:

WebBrowser webBrowser = new WebBrowser();
webBrowser.setSize(700,500);

This creates a new Web browser component (in the form of a window) and sets its size to 700,500. The WebBrowser class is a part of the package org.jdesktop.jdic.browser and hence the line to import this package at the top of the code-'import org.jdesktop.jdic.browser.*;'. 

The Web browser component is a subclass of the standard Java component class and hence can be added to the JFrame in the same way as in the case of other components-JPanel, JButton and JTextArea. 

Container contentPane = frame.getContentPane(); 
contentPane.add(webBrowser, BorderLayout.CENTER);

The code sums up with setting up the URL to be displayed on the Web browser window. This is done by constructing a URL as:

URL url = null;
try { 
url = new URL("http://www.pcquest.com"); 
}
catch (MalformedURLException e) {
System.out.println(e);

And using the setURL( ) method of the WebBrowser class as:

webBrowser.setURL(url);

See it running
Install JDK 1.4.2 from this month's PCQuest Essential CD. Add the bin directory of the JDK 1.4.2 (typically C:\\j2sdk1.4.2_05\\bin) to the path. Create a directory, say ieinjava, and copy the above code to this directory as IEInJava.java. 

With JDIC you can display a Flash-enabled website (such as nirulas.com) in your Java application, provided the Flash plug-in is installed for IE 

Next, to download the JDIC package go to https://jdic.dev.java.net/servlets/ProjectDocumentList. Click on jdic-0.8.4 and download the file named jdic-0.8.4.1-bin-windows.zip. Unzip the archive and copy the files named jdic.dll, IeEmbed.exe, nspr4.dll and jdic.jar to the directory ieinjava. Also, copy the file named jawt.dll found in the directory j2sdk1.4.2_05\\jre\\bin to ieinjava directory. Compile the code as:

javac -classpath jdic.jar;. IEInJava.java

And execute it as:

java -cp jdic.jar;. IEInJava

This will show up a window that will eventually load up www.pcquest.com (the URL specified to the setURL( ) method).

A Web browser listener
If you want to know what is happening to a Web page download, you will have to use the WebBrowserListener interface. Below is the description of some of the event handlers provided by this interface.

void downloadCompleted(WebBrowserEvent event) : Invoked when a download operation finishes, is halted, or fails. 
void downloadError(WebBrowserEvent event): Invoked when an error occurs during a download operation. 
void downloadProgress(WebBrowserEvent event) : Invoked when the progress of a download operation is updated. 
void downloadStarted(WebBrowserEvent event) : Invoked when a download operation is beginning

To see the above handlers at work, compile and run (as explain above) the following code. An output saying 'Download Completed', 'Download Error', 'Download in Progress' and 'Download Started' will be displayed at the Windows command prompt when the event handlers are invoked. 

import org.jdesktop.jdic.browser.*;
import java.awt.*;
import javax.swing.*;
import java.net.*;
public class IEInJava implements WebBrowserListener{
public static void main(String[] args) {
WebBrowser webBrowser = new WebBrowser();
webBrowser.setSize(700,500);
JFrame frame = new JFrame("IE in Java"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane(); 
contentPane.add(webBrowser, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
URL url = null;
try { 
url = new URL("http://www.pcquest.com"); 
}
catch (MalformedURLException e) {
System.out.println(e);

webBrowser.setURL(url); 
webBrowser.addWebBrowserListener(new IEInJava());
}
public void downloadCompleted(WebBrowserEvent event) {
System.out.println("Download Completed"); 
}
public void downloadError(WebBrowserEvent event) {
System.out.println("Download Error"); 

public void downloadProgress(WebBrowserEvent 
event) {
System.out.println("Download in Progress"); 
}
public void downloadStarted(WebBrowserEvent event) {
System.out.println("Download Started"); 
}
}

In the above code we have specified that the IEInJava class will implement the methods of the interface WebBrowserListener. Subsequently, we set up a WebBrowserListener in the typical way (as for other Java event listeners) as:

webBrowser.addWebBrowserListener(new IEInJava( ));

JDIC can be used to do more than display browsers. To explore more, go to https://jdic.dev.java.net/.

Shekhar Govindarajan
IT4Enterprise

  • Follow PCQuest on
  • become a fan on
  • Stay updated via
  • RSS

LEAVE A REPLY

Notify me of follow-up comments via e-mail address

Post Comment

Survey Box

Now that Microsoft has finally discontinued support for Windows XP, which OS are you likely to upgrade to?

Send this article by email

X