MorkaLork Development

Interesting stuff I've picked up over the years...

Creating your first MIDlet

2009-06-16 16:15:07 | 393 views | MIDlet java application startapp() destroyapp() pauseapp()

Your first MIDlet



Right, in the first two articles in this series we've seen how to install and open NetBeans, create a project and implement the basic methods needed to run a mobile application (startApp(), pauseApp() and destroyApp()).

What we are going to do now is actually create a MIDlet (or mobile application) that does something. Our MIDlet will output a form with a title and a message to the user.

Continuing from the last application from this article we have created a project that we call MorkaMobile (just for the sake of it) and added a file called MorkaMIDlet which contains our MIDlet implementation, it looks like this:



package org.morkalork.testExample;

import javax.microedition.midlet.*;

/**
* @author maffelu
*/
public class MorkaMIDlet extends MIDlet {

public void startApp() {
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

}


This application doesn't really do anything at all yet. What we are going to do is the following:



First we're gonna need to import some packages:



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

[invDivClosed]
More information on javax.microedition.midlet: link
More information on javax.microedition.lcdui: link
More information on javax.microedition.lcdui.StringItem: link
More information on javax.microedition.lcdui.Form: link
[/invDivClosed]

With these packages we nog get access to the forms from the lcdui that are designed to run on a mobile phone.

Before adding a form though you have to set a display. You can make MIDlets that just run in the background and then you wouldn't need to interact with the display, but since we are going to output something, we'll need one.

You'll need to create an instance of the Display class by using the Display method getDisplay which take the current MIDlet as a paremeter.



private Display display;
display = Display.getDisplay(this);


Now that we have a display we also need to create a form to put controls on:



private Form myForm;


We'll add a StringItem in the contructor of our MorkaMIDlet class:



//Contructor
public MorkaMIDlet(){
StringItem strItem = new StringItem("This is a string item", "");
myForm = new Form("This is a title");
myForm.append(strItem);
}


Now all we need is a start sequence for the startApp() method so that the display object we created is set to represent the current screen:



public void initMIDler(){
display = Display.getDisplay(this);
display.setCurrent(myForm);
}

public void startApp() {
if(display == null)
initMIDler(); //Run the initMIDler method
}


If we run the application now, this is what we can see:

Left: Selection screen, Right: Our MIDlet running

First we get a list of application to run, in this case we can only see MorkaMIDlet since it's our only application. When we press the Launch button the screen changes to show our form.

As we can see the form has a title and a string item. We have just run our first application and it should work flawlessly =D.


Full code





package org.morkalork.testExample;

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

/**
* This MIDlet will output a form with a title and a
* string item on it.
*
* @author maffelu
*/
public class MorkaMIDlet extends MIDlet {

private Display display;
private Form myForm;

//Contructor
public MorkaMIDlet(){
StringItem strItem = new StringItem("This is a string item", "");
myForm = new Form("This is a title");
myForm.append(strItem);
}

public void startApp() {
if(display == null)
initMIDler(); //Run the initMIDler method
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void initMIDler(){
display = Display.getDisplay(this);
display.setCurrent(myForm);
}
}



Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.