Accessing Internet Explorer from Java Swing

Accessing Internet Explorer from Java Swing

Java/J2EE COM Interoperability Products Page

This example demonstrates how to embed Internet Explorer (IE) in a Java Swing container. J-Integra® for COM is a Java interoperability component that bridges Java and Internet Explorer. It provides bi-directional access of Java objects and COM components.


An ActiveX Control is a kind of COM component that generally requires hosting in a GUI container. ActiveX Controls typically have the extension '.ocx'.

J-Integra® lets you embed a COM ActiveX Control inside a Java GUI as though the ActiveX Control were a Java GUI component. It lets you access the control's methods and properties and subscribe to its events using the standard Java mechanisms.

Note that using a Java class generated by 'com2java' with the option to generate AWT classes from ActiveX Controls set, J-Integra® will run in native mode automatically.

This example uses Borland/Inprise JBuilder 3.5, an evaluation copy of which is available for download.

We are investigating an issue with using JBuilder 4.0 and ActiveX controls in design mode.

The Steps Involved

  1. Set your PATH to include the J-Integra® 'bin' director
  2. Create the JBuilder project
  3. Run your new application

Set your PATH to include the J-Integra® 'bin' directory

Go to the Control Panel under Windows, and double-click on the "System" item. Go to the panel which lets you set environment variables (Under Windows 2000 it is under "Advanced") and edit the System "Path" environment variable to include the JDK \bin and \jre\bin directories and the J-Integra® \bin directory (for example "C:\JBuilder9\jdk1.4\bin;C:\JBuilder9\jdk1.4\jre\bin;C:\jintegra\bin").

Create the JBuilder project

Create and set up a new project

  1. Under Windows create a new directory for a new JBuilder project, such as "d:\pure\jb". In JBuilder, click on "File|New Project" to create a new project. Set the project file to "D:\pure\jb\jbie.jpr" and click on "Finish":

    Accessing Internet Explorer from Java Swing: Create new project

  2. Click on Project|Project Properties, go to the "Required Libraries" tab, click on "Add..." , Click on "New...", stay on the "Class" tab, click on "Add...", then browse to the "jintegra.jar" file in the J-Integra® 'lib' directory:

    Accessing Internet Explorer from Java Swing: Edit project details

Enter "J-Integra® Runtime" in the "Name:" field, click on "OK" in the three dialogs that are open, to return to the IDE.

Create a new application within the project

  1. Click on "File|New..." and select "Application" from the list, and click on "Finish" to create a new simple GUI application:

    Accessing Internet Explorer from Java Swing: Create new GUI application

Run 'com2java' to generate the proxies for Internet Explorer, and add them to the project

  1. Under Windows, create a new "D:\pure\jb\src\shell" directory into which the proxies will be generated.

  2. Start the J-Integra® 'com2java' tool, which will be in \jintegra\bin\com2java.exe. Click on the Select button to select shdocvw.dll as the type library, which by default is in \WINNT\system32\shdocvw.dll on the Windows drive under Windows NT.

  3. Next click on the Options button and check the Generate Java AWT Classes checkbox (don't forget to uncheck it later). Click on OK to leave the Options dialog and return to the main dialog. Do not check this option if your Java client does not embed an ActiveX Control.

  4. Finally enter shell as the Java Package in the main dialog, and then click on the Generate Proxies ... button, and select the D:\pure\jb\src\shell directory you created above (or its equivalent) as the output directory.

This will have generated a Java class which incorporates all the methods that Internet Explorer contains, as well as event classes and an event adapter which can be used to allow Java clients to listen to all of the events that Internet Explorer can generate.

  1. In JBuilder, right-click on "jbie.jpr" on the top, left-hand side of the IDE, select "Add to project" from the popup menu, and select "Add class/package..." from the second popup. Select "shell" from the list. If it is not in the list them you have not generated the proxies in the right place, or with the right package name.

  2. Click on "Project|Rebuild Project jbie.jpr".

Design the new application

  1. Click on the "Design" tab at the bottom of the screen to move the IDE into design mode, and make sure that the Frame class is selected.

  2. Select the "Swing Containers" palette page, and click on the first item ("JPanel"). Create a JPanel at the bottom of the canvas. Make sure its "Constraints" property is set to "South", and that its "layout" property is set to "FlowLayout".

  3. Click on the "Swing" palette page, and click on the "JTextField" item (the one to the right of the "label" item, and create a JTextField inside the JPanel you just added. Set the JTextField's "columns" property to "30", and set its "text" property to nothing.

  4. Click on the JButton item in the palette (the first item in the palette), and create a button inside the JPanel to the right of the text box. Set its "text" property to "Go".

  5. Click on "Tools|Configure Palette...", select the "Add Components" pane, select "AWT" under "Select palette page", and click on "Install...". Select the "Shell" package, and the "WebBrowser" class. Click on OK to return to the IDE, and select the "AWT" palette. On the far right will be the new component. Click on it and drag a large rectangle on the main canvas above the JPanel. You should now see something like this:

    Accessing Internet Explorer from Java Swing: Configure palette

  6. Select the "Events" pane (next to "Properties"). You will see all the COM events that Internet Explorer can generate. Double-click on box to the right of the "navigateComplete" method. You will be taken to the code editor to edit the code that is executed when a web page is displayed inside IE. Enter the code in bold in the event handler. It simply displays the URL that has been navigated to:

    void webBrowser1_navigateComplete(DWebBrowserEventsNavigateCompleteEvent e) {
      System.out.println("Navigate complete: " + e.getURL());

  7. Return to the GUI designer by clicking on the "Design" button, and double-click on the JButton you added to edit the code executed when the button is clicked on. Enter code to tell the embedded web browser to navigate to the URL entered in the text field:

    void jButton1_actionPerformed(ActionEvent e) {
      try {
        webBrowser1.navigate(jTextField1.getText(), null, null, null, null);
      } catch( ioe) {
        throw new RuntimeException(ioe + "");

Run your new application

Click on "Run|Run Project" to run the application you have just created. Enter a URL in the text box, and click on "Go".

The appropriate web page will be displayed and a text message will be output when the "navigateComplete" event is generated:

Accessing Internet Explorer from Java Swing: Run the application

If you get a NullPointerException when programming your own Java GUI which embeds an ActiveX Control, please refer to J-Integra® Knowledge Base article 30945.