[an error occurred while processing this directive]

SWT Browser

                         

This section shows you SWT Browser

In SWT, the class Browser of package org.eclipse.swt.browser allows to create a browser. In the given example, the class Browser implements the browser and allows the user to navigate through HTML documents. The class Text creates the text field for the URL. 

The Listener class is called to perform the action on ToolItems. The method browser.back() navigate to the previous session, the method browser.forward() navigates to the next session, the method browser.refresh() refresh the current page, the method browser.stop() stops the loading and rendering activity. The method browser.setUrl(text.getText()) loads the URL by getting the text from the text box.

Following code loads the given URL:

browser.setUrl("http://www.roseindia.net");

 

Here is the code of BrowserExample.java

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.browser.Browser;

public class BrowserExample{
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Browser Example");
    shell.setSize(500, 350);

    ToolBar toolbar = new ToolBar(shell, SWT.NONE);
    toolbar.setBounds(5, 5, 350, 30);
    ToolItem item1 = new ToolItem(toolbar, SWT.PUSH);
    item1.setText("Back");
    ToolItem item2 = new ToolItem(toolbar, SWT.PUSH);
    item2.setText("Forward");
    ToolItem item3 = new ToolItem(toolbar, SWT.PUSH);
    item3.setText("Refresh");
  ToolItem item4 = new ToolItem(toolbar, SWT.PUSH);
    item4.setText("Stop");
    ToolItem item5=new ToolItem(toolbar, SWT.PUSH);
  item.setText("Go");
    final Text text = new Text(shell, SWT.BORDER);
    text.setBounds(10, 35, 500, 25);
   
    final Browser browser = new Browser(shell, SWT.NONE);
    browser.setBounds(5, 75, 600, 400);

    Listener listener = new Listener() {
      public void handleEvent(Event event) {
        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());
    }
    };
    item1.addListener(SWT.Selection, listener);
    item2.addListener(SWT.Selection, listener);
    item3.addListener(SWT.Selection, listener);
  item4.addListener(SWT.Selection, listener);
  item5.addListener(SWT.Selection, listener);
    text.addListener(SWT.DefaultSelection, new Listener() {
      public void handleEvent(Event e) {
        browser.setUrl(text.getText());
      }
    });
    shell.open();
    browser.setUrl("http://www.roseindia.net");
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}


Output will be displayed as:


Download Source Code

                         

[an error occurred while processing this directive]