Home Tutorials Swt SWT Browser
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

SWT Browser

Advertisement
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.

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

     

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 24, 2008

Related Tutorials

Ask Questions?    Discuss: SWT Browser   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Rishi Ramakrishna Hegde
May 14, 2012
Swt Browser

Hi every one. Here is an advanced swt browser that you all might me interested in. go to the link below and read the details and download the swt browser. LINK --> http://paratechies.weebly.com/para-browser-v20.html
Davide
July 29, 2012
Typo in the code

item.setText("Go"); should be item5 BTW thanks for the nice example, it was very useful to me !
DMCA.com