Sunday, March 14, 2010

J2ME - Form - Code Example

This example will show you how simple it is to make J2ME Forms. All you have to do is create a MIDlet and in the startApp() method instantiate a Form and show a "Hello" string on top of it. If you have the development environment properly set up, this should take a few moments.

As you have read in the previous articles about the MIDlet life-cycle, startApp() is the method that is called by the J2ME runtime on the device and you can write your initializing code in this MIDlet method.
 
Following is a simple code chunk that will show a Form with a String on it.

How to create a Form in J2ME

In this code, on line 1 we create an instance of the Form. "Form Title" is obviously the text that will show as the title of the form on the screen. On line 2, we append a string to be shown on the form and the last line is making the Form visible on the screen.

        Form f = new Form("Form Title");
        f.append("Hello - This is the form");
        Display.getDisplay(this).setCurrent(f);


Here is the full code MIDlet Example


import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class FormExample extends MIDlet {

    protected void startApp() throws MIDletStateChangeException {
        Form f = new Form("Form Title");
        f.append("Hello - This is the form");
        Display.getDisplay(this).setCurrent(f);
    }
   
    public FormExample() {   
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
    }

    protected void pauseApp() {
    }
}




Output of the above code:







J2ME Form code example








Related Articles:



No comments:

Post a Comment