Saturday, February 20, 2010

Other Displayable Objects

Since I had a great deal of work i couldn't write this blog last few days. Today we'll look in to other displayable objects which we discussed earlier. This is just to let you guys know about these displayable objects. After this we'll go in to Commands which I will have to explain a lot (I think).
Ok since you have your FirstMidlet class you can comment out these lines

Alert alert = new Alert("Alert");
alert.setString("Hello World");
Display.getDisplay(this).setCurrent(alert);

and put this one and try:

TextBox tb = new TextBox("Text", "", 1000, TextField.ANY);/*Try changing TextField.ANY to other options as well*/
Display.getDisplay(this).setCurrent(tb);

now comment above and try this:

/*There are 3 types of list you can check them out by changing List.EXCLUSIVE to List.IMPLICIT n List.MULTIPLE*/
List lst = new List("List", List.EXCLUSIVE);
Display.getDisplay(this).setCurrent(lst);

lastly try this one:

Form frm = new Form("Form");
TextField tf = new TextField("Text Field", "", 100, TextField.PHONENUMBER);/*You can use ANY or others as well but I used this one because this is the way to access your phonebook through a textfiled*/
DateField df = new DateField("Date Field", DateField.DATE_TIME);
StringItem si = new StringItem("String", "");
si.setText("This is string item");
frm.append(tf);
frm.append(df);
frm.append(si);
Display.getDisplay(this).setCurrent(frm);

Ok now this settles it. Now it's time to get into real business with commands.

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.




Creating Your First Midlet

Hope you have your project created now, named FirstApp.
So now right click on project FirstApp and you will get a pop up menu in that menu,
New -> MIDlet
Now you will get a dialog box. There in midlet name type FirstMidlet and in package just type src for the time being. Click Finish.
Ok now you got your MIDlet which will look like this

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package src;

import javax.microedition.midlet.*;

/**
* @author Amila
*/
public class FirstMidlet extends MIDlet {
  public void startApp() {
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }
}

This is what we called midlet. The main class of the application. Just see that class has extended MIDlet class which is abstract. So three method are from MIDlet class
startApp() is your main method like in j2se. In j2me you don't have a main method instead of main method you have startApp as main method.
pauseApp() is the method which will get called from Application Management System(AMS) when application need to pause. eg. When mobile phone gets a call this function will be called.
destroyApp() this method is there to exit from the application. you can write your resource releasing kind of things in here.
So now you have created your first midlet and also have some knowledge about the midlets.

Begin Developing Mobile Applications

I hope by now you have installed jdk and netbeans successfully.
Ok now open up your netbeans first.
Then go to file -> New Project
Select Java ME in Categories and select Mobile Application on Project list. Click Next
There you can type your project name. So I will put FirstApp
And remember to untick the Create Hello Midlet. Click Next
There you will be asked which configuration and which profile just pick CLDC 1.0 and MIDP 2.0.
Click Finish.
(If it is successful you should get a project created on project tab on left side of workspace. If project tab is not there, go to Window menu and select project)

Ok now you have created a Java ME project successfully. So now you can start developing for mobiles.

First Step to J2ME

Ok this blog is there for you to learn about j2me, if you are totally new to the technology. Not to the java but to Java to Mobile Edition.
First of all you must have a knowledge in Java to learn about j2me. Because j2me is a subset of java.
Ok before beginning first you must install java in your system and to develop you can install NetBeans IDE.(or you can use Eclipse but I will use NetBeans in these tutorials)

Download Java Development Kit

Download NetBeans IDE (You can download which stated as java)