Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Using eSWT Browser in Java ME

From Wiki
Jump to: navigation, search

This code snippet demonstrates how to use eSWT's Browser class in Java ME.

Article Metadata
Code ExampleTested with
Devices(s): S60 3rd Ed. FP2 SDK
CompatibilityArticle
Keywords: eSWT, Browser, QueryDialog, MobileShell, MessageBox
Created: jarmlaht (20 May 2008)
Updated: skalogir (20 Aug 2012)
Reviewed: skalogir (20 Aug 2012)
Last edited: hamishwillee (31 Jul 2013)

Contents

Overview

The Browser class instance implements the browser user interface metaphor and it allows the user to visualize and navigate through HTML documents. This example has also "Back", "Forward", and "Open URL" Commands for navigating to the previous or next session history items and for opening desired HMTL page. The MIDlet uses MobileShell, because it has a possibility to change the status pane size. The following section contains the complete code for compiling and running the example application.

Source code

import javax.microedition.midlet.MIDlet;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ercp.swt.mobile.MobileShell;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.browser.Browser;
import org.eclipse.ercp.swt.mobile.QueryDialog;
import org.eclipse.ercp.swt.mobile.Command;
 
 
public class BrowserMIDlet extends MIDlet implements SelectionListener, Runnable {
private Command exitCommand;
private Command backCommand;
private Command openCommand;
private Command forwardCommand;
private Command changeCommand;
private Display display;
private MobileShell shell;
private Thread uiThread;
private Browser browser = null;
private boolean exiting = false;
private int style = 2; //MobileShell.SMALL_STATUS_PANE
private String url = "http://www.google.com";
 
public void startApp() {
if(uiThread == null) {
uiThread = new Thread(this);
uiThread.start();
}
}
 
public void pauseApp() {
//To do
}
 
public void destroyApp(boolean unconditional) {
exitEventLoop();
try {
uiThread.join();
}
catch(InterruptedException e) {
//To do
}
}
 
void exitEventLoop() {
exiting = true;
Display.getDefault().wake();
}
 
public void run() {
display = new Display();
shell = new MobileShell(display, SWT.NONE, style);
shell.setLayout(new FillLayout());
exitCommand = new Command(shell, Command.EXIT, 0);
exitCommand.setText("Exit");
exitCommand.addSelectionListener(this);
backCommand = new Command(shell, Command.BACK, 0);
backCommand.setText("Back");
backCommand.addSelectionListener(this);
forwardCommand = new Command(shell, Command.OK, 0);
forwardCommand.setText("Forward");
forwardCommand.addSelectionListener(this);
openCommand = new Command(shell, Command.OK, 1);
openCommand.setText("Enter URL");
openCommand.addSelectionListener(this);
changeCommand = new Command(shell, Command.OK, 2);
changeCommand.setText("Change status pane");
changeCommand.addSelectionListener(this);
try {
browser = new Browser(shell, SWT.NONE);
} catch (SWTError e) {
MessageBox message = new MessageBox(shell, SWT.NONE);
message.setText("SWTError: " + e.getMessage());
message.open();
}
if (browser != null) {
browser.setUrl(url);
}
shell.open();
while(!exiting) {
if(!display.readAndDispatch()) {
display.sleep();
}
}
browser.dispose(); // dispose objects
shell.dispose();
display.dispose();
notifyDestroyed(); // exit MIDlet
}
 
public void widgetSelected(SelectionEvent e) {
if (e.widget.equals(exitCommand)) {
exitEventLoop();
}
if (e.widget.equals(backCommand)) {
if (browser.isBackEnabled()) browser.back();
}
if (e.widget.equals(forwardCommand)) {
if (browser.isForwardEnabled()) browser.forward();
}
if (e.widget.equals(openCommand)) {
QueryDialog dialog = new QueryDialog(shell, SWT.NONE, QueryDialog.STANDARD);
dialog.setPromptText("Enter URL:", "http://");
url = dialog.open();
if (url != null) {
browser.setUrl(url);
shell.redraw();
} else {}
}
if (e.widget.equals(changeCommand)) {
if (style < 3) style = style + 1;
else if (style == 3) style = 1;
shell.changeTrim(SWT.SHELL_TRIM, style);
}
}
 
public void widgetDefaultSelected(SelectionEvent arg0) {
//To do
}
}

Postconditions

When a BrowserMIDlet is opened, a browser window is shown on the screen. The browser will open the URL http://www.google.com.

See also

This page was last modified on 31 July 2013, at 06:11.
96 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×