You Are Here:

Community: Wiki

This page was last modified 12:14, 12 October 2008.

CS000974 - Using eSWT Browser in Java ME

From Forum Nokia Wiki



ID CS000974 Creation date May 21, 2008
Platform S60 3rd Edition, FP2 Tested on devices S60 3rd Ed. FP2 SDK
Category Java Subcategory eSWT, Browsing


Keywords (APIs, classes, methods, functions): eSWT, Browser, QueryDialog, MobileShell, MessageBox

Overview

This code snippet demonstrates how to use eSWT's Browser class in Java ME. 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 html 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://forumnokia.mobi";
    
    public void startApp() {
        if(uiThread == null) {
            uiThread = new Thread(this);
            uiThread.start();
        }        
    }
 
    public void pauseApp() {
    }
 
    public void destroyApp(boolean unconditional) {
        exitEventLoop();
        try {
            uiThread.join();
        } 
        catch(InterruptedException e) {
        }        
    }
    
    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) {
    }    
}

Postconditions

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

See also

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditFurlTechnocratiMagnoliaTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fCS000974E5fE2dE5fUsingE5feSWTE5fBrowserE5finE5fJavaE5fME45X qdcZpublisherQUxhttpE3aE2fE2fswE2enokiaE2ecomE2fidE2fc764fd1cE2d8b06E2d499aE2d9a6aE2d17c3903d5a65E2fforumE5fnokiaE5fcrawlerE5fagentX qdcZtitleQSxCS000974E20E2dE20UsingE20eSWTE20BrowserE20inE20JavaE20ME45E20E2dE20ForumE20NokiaE20WikiX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfnTypeZCommunityContentQ qdcZtypeQUqfnTypeZE52esourceQ qdcZtypeQUqfnTypeZWebpageQ qdcZtypeQUqfnTypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qrssZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qfnZdistributionQUxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2fX qfnZtopicQUqfnTopicZbrowsingQRqdcZtypeQUqrdfsZE52esourceQRqmarsZrelevanceQNx100X qfnZtopicQUqfnTopicZeswtQRqdcZtypeQUqrdfsZE52esourceQRqmarsZrelevanceQNx100X qfnZtopicQUqfnTopicZj2meQRqmarsZrelevanceQNx100X qfnZtopicQUqfnTopicZjavaQRqmarsZrelevanceQNx100X qfnZtopicQUqfnTopicZwebE5ftechnologyQRqmarsZrelevanceQNx100X qfnZtypeQUqfnTypeZCommunityContentQ qfnZtypeQUqfnTypeZE52esourceQ qfnZtypeQUqfnTypeZWebpageQ qfnZtypeQUqfnTypeZWikiContentQ qfnZupdatedQDx2008E2d10E2d02X qfnZuserE5ftagQSxbrowsingX qfnZuserE5ftagQSxeswtX qfnZuserE5ftagQSxjavaX qfnZuserE5ftagQSxjavaE2dmeX qfnZuserE5ftagQSxwebE2dtechnologyX qmarsZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfnTypeZCommunityContentQ qrdfZtypeQUqfnTypeZE52esourceQ qrdfZtypeQUqfnTypeZWebpageQ qrdfZtypeQUqfnTypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ