Hello (Form version)

From Mobile Application Design
Revision as of 22:01, 3 February 2007 by Deprimer (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

HelloMidletForm

To get you started and make sure that your environment is working nicely - I've created a fully featured MIDlet which has but one purpose in life - to say hello. The Hello world program has a long and distinguished pedigree in the world of programming and for us it will serve two main purposes:

  1. Verify configuration. Creating and running this simple program lets us know that we've installed the basic software and setup our environment correctly.
  2. Sanity check. I know how it is, you've been up for three days solidly working on your gestural controller / MySpace worm / life size matchstick version of Yoda. You've written this code which looks bug free, but nothing's working. Start at the beginning with the Hello World skeleton application to check your environment and slowly build up your application again.
  • This article assumes you're using Eclipse + EclipseME to run this example. Steps may be slightly different for other devices.

Creating a project

  • Right click anywhere in the blank space of the Package Explorer tab in Eclipse. Select New -> Project from the context menu. Select J2ME->J2ME Midlet Suite as the type of project.
  • Name your project something suitable - like HelloMidletForm, then click Next.
  • Choose your Device from the list. Anything will do, but if you have something exotic and you're feeling experimental - give it a go. Click Next again and then Finish.

Creating the class

  • Once your project has been created, right-click on the src folder and choose New->Class.</a>
  • Name your class HelloMidletForm or whatever you called your project, ensuring that you spell it exactly as you did earlier (including case). Ignore Eclipse if it tries to warn you against using the default package.
  • Once you've done that, copy and paste the text below into your file, overwriting anything that was there before (if you changed names, Eclipse may complain and ask you to update the name of the class). Save the file.
/**
 * HelloMidletForm
 * A simple program which demonstrates displaying a string in a Form. 
 * 
 * copyleft Michael Sharon 2006
 */

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class HelloMidletForm extends MIDlet implements CommandListener {

	private Form aForm;

	public HelloMidletForm() {
		aForm = new Form("HelloMidlet");
		aForm
				.append(new StringItem(null,
						"Hello, MIDP! \n\nYou and me - we're gonna make sweet MIDlets together! "));
		aForm.addCommand(new Command("Exit", Command.EXIT, 0));
		aForm.setCommandListener(this);
	}

	public void startApp() {
		Display.getDisplay(this).setCurrent(aForm);
	}

	public void pauseApp() {
	}

	public void destroyApp(boolean unconditional) {
	}

	public void commandAction(Command c, Displayable s) {
		notifyDestroyed();
	}
}

Setup the JAD file

  • Double-click the HelloMidletForm.jad file in your Package Explorer. It should open up the EclipseME JAD editor.
  • Click on Midlets, then Add. Type HelloMidletForm in the Name field, skip the Icon field and head straight to the Class field. A button should appear, allowing you to select your HelloMidletForm source file.

Create a launch configration

  • Right-click your project again, choose Run->Run As from the context menu. Double click Wireless Toolkit Emulator to create a new configuration and name it something like Vanilla MIDlet.

The selected project should be HelloMidletForm. Click on the Midlet radio button under the Executable option and then click Search. It should easily find your default HelloMidletForm

  • Click Apply, then Run.

Troubleshooting

  • Whoa! Something exploded! Not cool. If you see something like this - that's fine, it just means that we forgot to compile our source file into a JAR file.
  • Right-click your project folder again and choose J2ME -> Create Package

Success!

  • Click the big Green Run button again. This time it should show signs of life and boot up your first Java program in whichever emulator you selected.