October 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Introducing a Lightweight UI Toolkit: Bringing Desktop Development into Java ME

  • July 17, 2008
  • By Ibon Urrutia
  • Send Email »
  • More Articles »

At this moment, you can download only a binary version of LWUIT (you are going to use version 20080625) in [1]; the source code is supposed to be downloadable when version 1.0 is be released. This binary has a Sun License Agreement; thish allows you to use it on commercial products for free; the source code is licensed under the well-known by Java developers GPLv2 with the classpath exception.

Before starting, you will add the LWUIT library to Netbeans; that way, all mobile projects developed inside your IDE can use it. Go to Tools > Libraries, and add the LWUIT jar you downloaded from [1] on the Classpath tab. If you add javadoc in the corresponding tab, you will enjoy the integrated help of the Netbeans editor.



Click here for a larger image.

Figure 2: Libraries dialog showing Javadoc tab

Creating the Midlet

You have to create a new Mobility project (New Project > Mobility > MIDP Application); the only thing to take care of is to select MIDP-2.0 as a "Device Profile" in the "Default Platform Selection" display because you want to cover the majority of current phones. In the "Resources" node, select "Add Library" and select LWUIT from that dialog. At the end, you will have a LWUIT-capable mobile app.

You have all the code at the end of this article, packaged as a Netbeans mobility project. But let me start with a very simple approach to show you what the appearance of your LWUIT applications will be when you start them from scratch. As I said, LWUIT only covers lcdui classes, so your MIDlet will be a normal MIDlet, with some new additions to MIDP:

public void startApp() {
   ...
   // With LWUIT, you always have to do this
   Display.init(this);

   // Form is a display
   Form mainMenu = new Form("Yahoo Weather with LWUIT");
   // Like Desktop apps, it needs a Layout
   mainMenu.setLayout(new BorderLayout());

   // We need a Container to place buttons
   Container mainContainer =
      new Container(new BoxLayout(BoxLayout.Y_AXIS));

   // Some buttons that define the main menu
   getWeatherButton = new Button("Get Weather!");
   setPlaceButton   = new Button("Set Place");

   // As in Java SE, somebody has to listen and act.
   // It is declared as a private class.
   ButtonActionListener buttonListener =
      new ButtonActionListener();
   getWeatherButton.addActionListener(buttonListener);
   setPlaceButton.addActionListener(buttonListener);

   // Adding components to containers
   mainContainer.addComponent(getWeatherButton);
   mainContainer.addComponent(setPlaceButton);
   mainMenu.addComponent(BorderLayout.CENTER, mainContainer);

// Add soft buttons to the form
   mainMenu.addCommand(exitCommand);
   mainMenu.addCommand(aboutCommand);
   mainMenu.setCommandListener(this);
   // Finally, show it on screen
   mainMenu.show();

...

The first interesting thing is that you always have to pass a Midlet reference to the Display class, before showing any Form on the screen. Display is a very important class that controls the Event Dispatch Thread: a thread where all events (key pressed, key released, painting, and so forth) happens; this is very similar to the Swing EDT. I will speak more about EDT in the next article, when you focus on animations.

Don't confuse the LWUIT Form class with the lcdui one. Form is the top-level container of the UI and it also, as lcdui one, controls and shows one screen. But, it differs mainly from its grandfather in the fact that it can hold not only widgets but also containers, and that you only need to call its show() method to paint it onscreen.


Tags: mobility



Page 2 of 7



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel