Recently I have been experimenting J2ME with my Sony Ericsson w710i. The simplicity of J2ME application is amazing.
Let’s create a J2ME project first. Hope you have already installed the Sun Java Wirless Toolkit for CLDC. Launch the CLDC KToolbar application. Select New Project. Give HelloWorld as Project Name and MIDlet Class name. It will show the Settings screen, go to the Required section and set the MicroEdition-Configuration and MicroEdition-Profile as per your phone. Select OK.
The KToolBar would have generated your application folder structure inside the apps folder of your installation, if it is not there check out in the Documents and Settings folder, under your <login> folder it would have created the j2mewtk folder and stored you application there. You have to put all you sources in the <app_name>\src directory.
Let’s create the main application class file. Create a text file in the name of HelloWorld.java in the src folder. It is really simple, you just need to have a class that extends the MIDlet class, now you have to implement the three basic function startApp, destroyApp, pauseApp. As the name suggests these are called when you Start, Destroy or Pause your application.
import javax.microedition.midlet.*;The function startApp() is called when you launch your application, the destroyApp() function is called when you close is application, notifyDestroyed() is called to notify the system that the application is closed. The pauseApp() function is called when the application is pause by System due to a interruption like incoming call or sms.
import javax.microedition.lcdui.*;
public class HelloWorld extends MIDlet
{
private Display display;
protected void startApp()
{
display = Display.getDisplay(this);
}
protected void destroyApp(boolean unconditional)
{
notifyDestroyed();
}
protected void pauseApp()
{
}
}
The Display class is the highest object in the application, using display class you can show a Form in the screen.
The basic screen component in J2ME is a Form. A Form can show UI Components, a Form can be shown / hide. Let’s add a Form to the application.
That’s it. Now we have added a Form with a title “Sample Application” and text “Hello World”. The form will be displayed when the display.setCurrent(mForm); is called.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class MyFirstApp extends MIDlet
{
private Display display;
private Form mForm;
protected void startApp()
{
display = Display.getDisplay(this);
mForm = new Form("Sample Application");
mForm.append("Hello World");
display.setCurrent(mForm);
}
protected void destroyApp(boolean unconditional)
{
notifyDestroyed();
}
protected void pauseApp()
{
}
}
Now you can Build the application and Run the application in the simulator. To see your app in the phone select Project->Package->Create Package, it will create the jad and jar file in the <app_name>\bin folder. Download the jar file to your phone via cable or Bluetooth and select the file in the phone. Follow the steps given in your phone to install and run the application.