Opening Internet Explorer in anSWT Window

As you can see, SWT is dedicated to covering the bases when it comes to GUI development, giving you the controls you'd expect to find in standard GUIs. That also means SWT work can be pretty pedestrian, as you stock your GUI with standard controls like sliders, text controls, and so on. So we'll end the SWT discussion with an SWT example that's cooler than most—in this case, we'll see how to open Internet Explorer inside an SWT window and browse to a URL of our selection. For example, the user may want to search for information on the Internet, in which case you could open a search engine. In our example, we'll assume the user needs some good computer books, so we'll browse to the site http://www.oracle.com. In this new project, Ch08_06, we'll use Object Linking and Embedding (OLE) to open Internet Explorer in an SWT window, which means this example will be Windows-only. Unfortunately, there is no counterpart in Linux or Mac OS X—yet. Still, it's worth looking at how this works in Windows because it's impressive.

Screenshot

On the other hand, browser widgets will be coming built-in to SWT in Eclipse 3.0—see the discussion in .


To work with OLE, you import the org.eclipse.swt.ole.win32.* packages and create an SWT OleControlSite object in the main method:

package org.eclipsebook.ch08;
import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.ole.win32.*;
public class Ch08_06 {
 public static void main(String[] args) {
 final Display display = new Display( );
 Shell shell = new Shell(display);
 shell.setSize(600, 400);
 shell.setLayout(new FillLayout( ));
 OleControlSite oleControlSite;
 .
 .
 .


The window we'll create will be an SWT OleFrame object, and we'll have Internet Explorer in place in that window. To do that, we'll customize the OleControlSite object to work with Internet Explorer, create a new OleFrame object, then execute the OLE verb OLE.OLEIVERB_INPLACEACTIVATE to start Internet Explorer. We'll also configure an OleAutomation object we'll call browser to let us interact with Internet Explorer:

OleControlSite oleControlSite;
OleFrame oleFrame = new OleFrame(shell, SWT.NONE);
oleControlSite = new OleControlSite(oleFrame, SWT.NONE, "Shell.Explorer");
oleControlSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
shell.open( );
final OleAutomation browser = new OleAutomation(oleControlSite);


To work with the browser object, you can use the getIDsOfNames method to get the OLE IDs for the browser's navigation verb, and you simply invoke that verb to navigate to http://www.oracle.com, as you see in Example 8-6.

Example 8-6. Opening Internet Explorer, Ch08_06.java
package org.eclipsebook.ch08;
import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.ole.win32.*;
public class Ch08_06 {
 public static void main(String[] args) {
 final Display display = new Display( );
 Shell shell = new Shell(display);
 shell.setSize(600, 400);
 shell.setLayout(new FillLayout( ));
 OleControlSite oleControlSite;
 OleFrame oleFrame = new OleFrame(shell, SWT.NONE);
 oleControlSite = new OleControlSite(oleFrame, SWT.NONE, "Shell.Explorer");
 oleControlSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
 shell.open( );
 final OleAutomation browser = new OleAutomation(oleControlSite);
 int[] browserIDs = browser.getIDsOfNames(new String[]{"Navigate", "URL"}); Variant[] address = new Variant[] {new Variant("http://www.oracle.com")};
 browser.invoke(browserIDs[0], address, new int[]{browserIDs[1]});
 while (!shell.isDisposed( )) {
 if (!display.readAndDispatch( ))
 display.sleep( );
 }
 browser.dispose( );
 display.dispose( );
 }
}


You can see the results in Screenshot-12, where we've opened Internet Explorer and used it to navigate to http://www.oracle.com. Pretty cool.

Screenshot-12. Using Internet Explorer in an SWT window
Java figs/ecps_0812.gif

That completes this example, and it completes our look at SWT in these two chapters. There's a great deal more to the topic, of course, more than we could cover here. But these two chapters give you a good handle on SWT and give you the technology you need to master the topic. The general technique is pretty simple: you create a Display object and Shell objects for the various windows you want to display, stock those shell objects with control, display the windows, and set up a message loop to handle events. It's a powerful GUI builder that's gaining popularity every day. For more on individual controls and how to work with them, take a look at the SWT documentation that comes with Eclipse.

      
Comments