LWUIT Tutorial

Hello, LWUIT!

To get started right away, follow these steps.

  1. Download the LWUIT JAR file to your computer.
  2. Create a new project. In NetBeans, choose File > New Project... from the menu. Choose the Mobility category and MIDP Application type. Press Next >.

    Now enter a name for the project. Here I've used HelloLWUIT. Choose a location and make sure to uncheck Create Hello MIDlet. Press Next >.

    Finally, choose either Sun Java(TM) Wireless Toolkit 2.5.2 for CLDC or Sprint Wireless Toolkit 3.2 Powered by Sun Java Technology (what a mouthful!) for the Emulator Platform. Choose your favorite device. Press Finish to create the project.

  3. Add LWUIT to the project. In the project you just created is an item called Resources. If you don't see this, make sure you are looking at the Projects view, which is accessible from Window > Projects in the menu.

    Right-click on Resources and choose Add Jar/Zip Navigate to the LWUIT JAR file. When you are finished, you should see the JAR file in the Resources section when you expand it.

  4. Write a simple application. Right-click on your project and choose New > MIDlet from the menu. Enter a name, like HelloLWUITMIDlet, and press Finish.

    Here is the source code for a simple LWUIT MIDlet. Type it in or copy and paste it.

    import javax.microedition.midlet.*;
    import com.sun.lwuit.*;
    import com.sun.lwuit.events.*;
    public class HelloLWUITMidlet extends MIDlet implements ActionListener {
        public void startApp() {
          Form f = new Form("Hello, LWUIT!");
          Command exitCommand = new Command("Exit");
        public void pauseApp() {}
        public void destroyApp(boolean unconditional) {}
        public void actionPerformed(ActionEvent ae) {
  5. Compile and run. In NetBeans, just press F6 to compile and run. A device emulator will pop up with your application. The text "Hello, LWUIT!" appears as the title of the form, and "Exit" is at the bottom of the screen. Congratulations!

    When you're finished admiring your work, press Exit or press F1 (the left soft button) to stop.

Copyright © 2008 Sun Microsystems, Inc.