Sunday, September 28, 2008

Hello World J2ME version

Before reading this, you should have downloaded Netbeans IDE 6.1, choose the Mobility version or the All version.

When creating a J2ME application, I usually separate my MIDlet class, view-controller classes and model classes.

I do not separate View and Controller since normally my views only have a couple of controls and I find it more organized that way.

Link for the source code at the end of the tutorial.

Step 1. Create a new Project By File>New Project
Click Mobility from the Categories and MIDP Application from Projects.


Step 2. Write any Project Name and select a project location then click the Next Button.
Step 3. Select CLDC 2.0 and MIDP 2.0

Step 4. Click Finish. Delete all generated .java files as we will not be using the WYSIWYG editor.

Then create two classes, HelloWorldForm.java and HelloWorldMIDlet.java
You should have the following files on your Project Tab.


Step5. Writing the HelloWorldMIDlet.java code.

PauseApp, destroyApp and startApp are the abstract classes of the MIDlet class that needs to be implemented. I added two methods to be used by the UI class, showScreen(Displayble d) and showScreenAndAlert(Alert a, Displayble d).

The code in startApp() will be discussed after the next step.


Step 6. Coding the HelloWorldForm.java.

As you may have noticed, the MIDlet class is passed to the constructor, I find this useful in creating a apps with a lot of forms. Further, the MIDlet class may also be used to store Application sessions data, also handling unexpected application problems.

The initComponents() method is where the UI Components are initialized and added to the Form.

If you do not want the controller and the view to be on the same class, you may create, or pass in the constructor a class that implements the CommandListener interface.

On the code, instead of setCommandListener(this), just replace this with a CommandListener instance.



Step 7. Building and Running.





Source Code

No comments:

Email

java.padawan@androidph.com