Wednesday, February 17, 2010

Creating Your First Midlet - Cont.

Ok now you have your mildet - FirstMidlet. So lets just write our famous Hello World.
First you need a displayable object. There are several displayable object in j2me which are

  • Alert Object
  • TextBox Object
  • List Object
  • Form Object
To view any of this displayable objects you need to have a display object. You can get it from
Display class using getDisplay(MIDlet arg) method.

And there are some item objects which can only fit into Form Objects. These items can't display alone. They have to have a Form to display. So the items are

  • ChoiceGroup Object
  • DateField Object
  • Guage Object
  • Image and ImageItem Object
  • StringItem Object
  • TextField Object

For our first program we will use Alert Displayable Object. Try this code putting inside startApp() method.

Alert alert = new Alert("Alert");//string is the title of the alert
alert.setString("Hello World");
Display.getDisplay(this).setCurrent(alert);

Ok if you see this line of code Display.getDisplay(this).setCurrent(alert); you must be wonder what is this setCurrent() is? Well that is the method which you can display and displayable objects into the mobile device screen.


After you put this code your startApp() will look like this
    public void startApp() {
        Alert alert = new Alert("Alert");
        alert.setString("Hello World");
        Display.getDisplay(this).setCurrent(alert);
    }



Ok now you have created your first mobile application. Now you can go to Run menu and click Run Main Project or just press F6.
wait few seconds and there you have your first mobile application which will similar to this.




1 comment: