Article ID: 179849
Article Last Modified on 11/14/2005
import java.awt.*;
import java.awt.event.*;
import com.ms.activeX.*;
import com.ms.com.*;
import shdocvw.*; // Import the IE40 package created by JactiveX.
public class JavaConnect extends Frame
implements DWebBrowserEvents2, ActionListener
{
IWebBrowser2 browser; // Member to hold IE COM object
TextField location; // Member on Frame for user to type in URL
TextArea out; // TextArea on Frame that shows Browser status text
ConnectionPointCookie cookie; // Cookie that connects your event
// "sink" to the browser
public static void main(String args[])
{
// Create an instance of this class. Resize and show.
final JavaConnect jc = new JavaConnect();
jc.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
if (jc.browser != null)
jc.browser.Quit();
}
});
jc.setSize(400,400);
jc.show();
}
public JavaConnect()
{
// Create an instance of IE and make it visible
browser = (IWebBrowser2)new InternetExplorer();
browser.setVisible(true);
// Create a ConnectionPoint between the browser
// and our DWebBrowserEvents2 interface
cookie = new ConnectionPointCookie(browser, this,
shdocvw.DWebBrowserEvents2.class);
// Create a Panel for the Button and TextField
Panel northPanel = new Panel();
northPanel.setLayout(new BorderLayout());
// Create a Button, add yourself as its ActionListener
// and add it to the Panel
Button nav = new Button("Navigate");
nav.addActionListener(this);
northPanel.add("West", nav);
// Create a TextField and add it to the Panel
location = new TextField();
northPanel.add("Center", location);
// Add Panel to Frame
add("North", northPanel);
// Create a TextArea and add it to the Frame
out = new TextArea();
add("Center", out);
}
// ***********************************************************
// These methods are declared in the ActionListener interface.
// By implementing this interface, you can receive events
// from the button on the Frame.
public void actionPerformed(ActionEvent e)
{
// Create a variant for use with the optional
// parameters of the Navigate2 method
Variant optional = new Variant();
optional.noParam();
// Call Navigate2 on IE
browser.Navigate2(new Variant(location.getText()),
optional, optional, optional, optional);
}
// ***************************************************************
// These methods are declared in the DWebBrowserEvents2 interface.
// By implementing this interface and using a ConnectionPointCookie,
// you can receive events from Internet Explorer.
// Each method represents a different event that can come from
// Internet Explorer.
public void StatusTextChange(String Text)
{
// The status bar information has changed.
// Append this new status text to the TextArea.
out.append(Text+"\n");
}
public void ProgressChange(int Progress, int ProgressMax)
{}
public void CommandStateChange(int Command, boolean Enable)
{}
public void DownloadBegin()
{}
public void DownloadComplete()
{}
public void TitleChange(String Text)
{}
public void PropertyChange(String szProperty)
{}
public void BeforeNavigate2(Object pDisp, Variant URL,
Variant Flags, Variant TargetFrameName, Variant PostData,
Variant Headers, boolean[] Cancel)
{}
public void NewWindow2(Object[] ppDisp, boolean[] Cancel)
{}
public void NavigateComplete2(Object pDisp, Variant URL)
{}
public void DocumentComplete(Object pDisp, Variant URL)
{}
public void OnQuit()
{
// The user closed IE.
out.append("Quiting IE\n");
cookie = null;
// Manually release the browser COM object.
// This is not necessary, but ensures that no extra
// references to the COM object hang around after we
// exit the Java application.
ComLib.release(browser);
browser = null;
System.exit(0);
}
public void OnVisible(boolean Visible)
{}
public void OnToolBar(boolean ToolBar)
{}
public void OnMenuBar(boolean MenuBar)
{}
public void OnStatusBar(boolean StatusBar)
{}
public void OnFullScreen(boolean FullScreen)
{}
public void OnTheaterMode(boolean TheaterMode)
{}
}
In the constructor, create a ConnectionPointCookie. The constructor parameters should be based on the following:
jview JavaConnectOne is a Java Frame that contains a TextField, a Button, and a TextArea. The other is the Internet Explorer windows. On the Java Frame, enter a URL in the TextField and click Navigate. You should notice the Internet Explorer window load the URL you specified. As Internet Explorer loads the Web page, the Status Bar at the bottom of the browser window changes to give the user an updated status. This text is also echoed to the Java application through the event mechanism. The applications StatusTextChange() method gets called each time. The StatusTextChange method appends the text to the bottom of the TextArea on the Frame.
177165 INFO: Use New Java 1.1 Language Features with Visual J++ 1.1
Additional query words: ConnectionPointCookie event sink
Keywords: kbhowto kbfaq kbjavafaq KB179849