[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:



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);
    ToolItem item2 = new ToolItem(toolbar, SWT.PUSH);
    ToolItem item3 = new ToolItem(toolbar, SWT.PUSH);
  ToolItem item4 = new ToolItem(toolbar, SWT.PUSH);
    ToolItem item5=new ToolItem(toolbar, SWT.PUSH);
    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"))
    else if (string.equals("Forward"))
    else if (string.equals("Refresh"))
     else if (string.equals("Stop"))
         else if (string.equals("Go"))
    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) {
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())

Output will be displayed as:

Download Source Code


[an error occurred while processing this directive]