As shown earlier in Figure 8.1, the Browser project uses ToolBar, Text, and Browser widgets. It starts by creating a good-sized shell to display the browser in: import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; public class BrowserProject { Display display; Shell shell; public static void main(String[] args) { new BrowserProject(); } BrowserProject() { display = new Display(); shell = new Shell(display); shell.setText("The Browser Project"); shell.setSize(600, 500); . . . } Then it creates the toolbar and buttons you see in Figure 8.1: import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; public class BrowserProject { Display display; Shell shell; ToolBar toolbar; ToolItem go, forward, back, refresh, stop; public static void main(String[] args) { new BrowserProject(); } BrowserProject() { display = new Display(); shell = new Shell(display); shell.setText("The Browser Project"); shell.setSize(600, 500); toolbar = new ToolBar(shell, SWT.NONE); toolbar.setBounds(5, 5, 200, 30); go = new ToolItem(toolbar, SWT.PUSH); go.setText("Go"); forward = new ToolItem(toolbar, SWT.PUSH); forward.setText("Forward"); back = new ToolItem(toolbar, SWT.PUSH); back.setText("Back"); refresh = new ToolItem(toolbar, SWT.PUSH); refresh.setText("Refresh"); stop = new ToolItem(toolbar, SWT.PUSH); stop.setText("Stop"); . . . } You'll also need a Text widget to let the user enter URLs, and you'll need the Browser widget itself. Here's what that looks like: import org.eclipse.swt.*; import org.eclipse.swt.browser.*; import org.eclipse.swt.widgets.*; public class BrowserProject { Display display; Shell shell; ToolBar toolbar; ToolItem go, forward, back, refresh, stop; Text text; Browser browser; public static void main(String[] args) { new BrowserProject(); } BrowserProject() { display = new Display(); shell = new Shell(display); shell.setText("The Browser Project"); shell.setSize(600, 500); . . . stop = new ToolItem(toolbar, SWT.PUSH); stop.setText("Stop"); text = new Text(shell, SWT.BORDER); text.setBounds(5, 35, 400, 25); browser = new Browser(shell, SWT.NONE); browser.setBounds(5, 75, 590, 400); . . . } You can find the significant methods of the SWT Browser class in Table 8.7. Note the methodssetUrl, back, forward, stop, and refreshthis project will use to make the browser do what the user wants.
You can call the Browser widget's methods matching the various buttons in the toolbar. Here's what that looks like in code, making the toolbar buttons active (note that the Go button's code reads the URL the user entered into the Text widget and calls the browser's setUrl method to navigate to that URL): import org.eclipse.swt.*; import org.eclipse.swt.browser.*; import org.eclipse.swt.widgets.*; public class BrowserProject { Display display; . . . public static void main(String[] args) { new BrowserProject(); } BrowserProject() { display = new Display(); shell = new Shell(display); shell.setText("The Browser Project"); shell.setSize(600, 500); . . . browser = new Browser(shell, SWT.NONE); browser.setBounds(5, 75, 590, 400); Listener listener = new Listener() { public void handleEvent(Event event) { try{ ToolItem item = (ToolItem) event.widget; String string = item.getText(); if (string.equals("Back")){ browser.back(); } else if (string.equals("Forward")){ browser.forward(); } else if (string.equals("Refresh")){ browser.refresh(); } else if (string.equals("Stop")){ browser.stop(); } else if (string.equals("Go")){ browser.setUrl(text.getText()); } } catch(Exception e) { System.out.println(e.getMessage()); } } }; go.addListener(SWT.Selection, listener); forward.addListener(SWT.Selection, listener); refresh.addListener(SWT.Selection, listener); back.addListener(SWT.Selection, listener); stop.addListener(SWT.Selection, listener); . . . } That makes the toolbar active; all the user has to do is to click a button to make the browser do what he wants. As with conventional browsers, you can also let the user navigate to a new URL simply by entering that URL in the text field and pressing Enter. Here's how that works in BrowserProject.java: text.addListener(SWT.DefaultSelection, new Listener() { public void handleEvent(Event e) { browser.setUrl(text.getText()); } } ); . . . } All that's left is to display the shell, including the Browser widget. When the browser first opens, it needs some default page to go to, and the Browser project simply uses http://www.sun.com: import org.eclipse.swt.*; import org.eclipse.swt.browser.*; import org.eclipse.swt.widgets.*; public class BrowserProject { Display display; Shell shell; . . . public static void main(String[] args) { new BrowserProject(); } BrowserProject() { display = new Display(); shell = new Shell(display); shell.setText("The Browser Project"); shell.setSize(600, 500); . . . shell.open(); browser.setUrl("http://www.sun.com"); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } } NOTE You can also integrate this browser window into a standard, otherwise non-SWT Java project with ease if you want towhen you need this browser window, just call the shell.open method, and the browser window will open. That completes the Browser project, BrowserProject.java. This browser features Go, Forward, Back, Refresh, and Stop buttonsand the user can even work with his Favorites folder, save images, print pages, and so on, simply by right-clicking the displayed web page, because what he's seeing in the browser control is Internet Explorer. NOTE Download the complete source code for the Browser project, BrowserProject.java, at the Sams website. After compiling the needed file (creating BrowserProject.class), you can run it, as detailed in this chapter, and get started browsing. You can also run the sample projects, ToolbarProject.java and LabelProject.java, following the instructions in this chapter. |