Accessing Internet Explorer from Java Swing |
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.
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").
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":
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:
Enter "J-Integra® Runtime" in the "Name:" field, click on "OK" in the three dialogs that are open, to return to the IDE.
Click on "File|New..." and select "Application" from the list, and click on "Finish" to create a new simple GUI application:
Under Windows, create a new "D:\pure\jb\src\shell" directory into which the proxies will be generated.
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.
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.
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 WebBrowser.java 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.
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.
Click on "Project|Rebuild Project jbie.jpr".
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.
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".
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.
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".
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:
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());
}
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(java.io.IOException ioe) {
ioe.printStackTrace();
throw new RuntimeException(ioe + "");
}
}
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:
|
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.