February 19, 2008

SWT - Browser

// Show the browser
final Browser browser = new Browser(shell, SWT.NONE);
browser.setSize(500,500);
browser.setLocation(10,60);
browser.setUrl("http://www.stanleygeorge.com");

// Prepare a toolbar to show the 'forward' and 'back' navigation buttons
ToolBar navBar = new ToolBar(shell,SWT.HORIZONTAL);
navBar.setSize(150,50);
navBar.setLocation(10,10);

// The back button
final ToolItem back = new ToolItem(navBar, SWT.PUSH);
back.setText("back");
back.setEnabled(false);
// Add a listener so that it will go to the previous URL in history
back.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
browser.back();
}
});

// The code for the forward button is similar to the above code
// Use browser.forward(); instead of browser.back();

// Enable/disable the buttons automatically
LocationListener locationListener = new LocationListener() {
public void changed(LocationEvent event) {
Browser browser = (Browser)event.widget;
back.setEnabled(browser.isBackEnabled());
forward.setEnabled(browser.isForwardEnabled());
}
public void changing(LocationEvent event) {
}
};

browser.addLocationListener(locationListener);
Reference: http://www.eclipse.org/articles/Article-SWT-browser-widget/browser.html

No comments: