Week 1: HelloMIDlet: Difference between revisions
From Mobile Application Design
Jump to navigationJump to search
mNo edit summary |
m Protected "Week 1: HelloMIDlet" [edit=sysop:move=sysop] |
||
| (One intermediate revision by the same user not shown) | |||
| Line 52: | Line 52: | ||
* Right-click your project folder again and choose J2ME -> Create Package | * Right-click your project folder again and choose J2ME -> Create Package | ||
* 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. | * 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. | ||
Latest revision as of 23:04, 25 November 2006
Hello MIDlet
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:
- Verify configuration. Creating and running this simple program lets us know that we've installed the basic software and setup our environment correctly.
- 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.
Tutorial
- 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 HelloMidlet, 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.
- Once your project has been created, right-click on the src folder and choose New->Class.</a>
- Name your class HelloMidlet 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. Save the file.
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class HelloMidlet extends MIDlet implements CommandListener {
private Form aForm;
public HelloMidlet() {
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();
}
}
- Double-click the HelloMidlet.jad file in your Package Explorer. It should open up the EclipseME JAD editor.
- Click on Midlets, then Add. Type HelloMidlet in the Name field, skip the Icon field and head straight to the Class field. A button should appear, allowing you to select your HelloMidlet source file.
- 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 HelloMidlet. Click on the Midlet radio button under the Executable option and then click Search. It should easily find your default HelloMidlet
- Click Apply, then Run.
- 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
- 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.