Creating the Browser Project

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 methods—setUrl, back, forward, stop, and refresh—this project will use to make the browser do what the user wants.

Table 8.7. Significant Methods of the org.eclipse.swt.widgets.Browser Class
MethodDoes This
void addCloseWindowListener (CloseWindowListener listener)Adds a close-window listener to the browser
void addLocationListener (LocationListener listener)Adds a location listener to the browser
void addOpenWindowListener (OpenWindowListener listener)Adds an open-window listener to the browser
void addProgressListener (ProgressListener listener)Adds a progress listener to the browser
void addStatusTextListener (StatusTextListener listener)Adds a status listener to the browser
void addTitleListener(TitleListener listener)Adds a close-title listener to the browser
void addVisibilityWindowListener (VisibilityWindowListener listener)Adds a visibility listener to the browser
boolean back()Navigates back to the previous web page in the browser's history
boolean forward()Navigates forward to the next web page in the browser's history
String getUrl()Returns the current URL
boolean isBackEnabled()Returns true if the browser can navigate back to the previous web page in the history
boolean isForwardEnabled()Returns true if the browser can navigate forward to the next web page in the history
void refresh()Refreshes the current web page in the browser
void removeCloseWindowListener (CloseWindowListener listener)Removes the given close-window listener from the browser
void removeLocationListener (LocationListener listener)Removes the given location listener from the browser
void removeOpenWindowListener (OpenWindowListener listener)Removes the given open-window listener from the browser
void removeProgressListener (ProgressListener listener)Removes the given progress listener from the browser
void removeStatusTextListener (StatusTextListener listener)Removes the given status text listener from the browser
void removeTitleListener (TitleListener listener)Removes the given title listener from the browser
void removeVisibilityWindowListener (VisibilityWindowListener listener)Removes the given window visibility listener from the browser
boolean setText(String html)Displays the given HTML text in the browser
boolean setUrl(String url)Makes the browser navigate to the given URL
void stop()Stops the current operation

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 to—when 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 buttons—and 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.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.10.113