Thursday, October 25, 2007

MIDP Application, Hello World 2, Using the Source Editor

1 - Choose File > New Project (Ctrl-Shift-N). Under Categories, select Mobility. Under Projects, select MIDP Application and click Next.
2 - Enter HelloWorld2 in the Project Name field. Change the Project Location to any directory on your system.
3 - Check the Set as Main Project checkbox and remove the check from the Create Hello MIDlet checkbox. Click Next.
4 - Leave the J2ME Wireless Toolkit as the selected Target Platform.
5 - Expand "Configuration templates provided by installed CLDC platforms" and J2ME Wireless Toolkit folders. Check the boxes next to each of the configurations.
The IDE will automatically creates a new project configuration for each template listed.
6 - Click Finish. The IDE creates the HelloWorld2 project folder. The project folder contains all of your sources and project metadata.

7 - Right-click the HelloWorld2 node in the Explorer window and choose New > MIDlet

8 - Enter HelloWorld2 as the MIDlet name. Click Finish. The HelloWorld2.java file is created.

9 - Double click the HelloWorld2.java file to display the source code in the Editor.

---------------------
10 - Change the code
public class HelloWorld2 extends MIDlet
to

public class HelloWorld2
extends MIDlet implements javax.microedition.lcdui.CommandListener
{
11 - Add the following text before the startApp() method:

public HelloWorld2() {
}
private void initialize() {
javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_helloTextBox());
}

public void commandAction(javax.microedition.lcdui.Command command, javax.microedition.lcdui.Displayable
displayable) {
if (displayable == helloTextBox) {
if (command == exitCommand) {
javax.microedition.lcdui.Display.getDisplay(this).setCurrent(null);
destroyApp(true);
notifyDestroyed();
}
}

}

private javax.microedition.lcdui.TextBox get_helloTextBox() {
if (helloTextBox == null) {
helloTextBox = new javax.microedition.lcdui.TextBox(null, "Test String",120, 0x0);
helloTextBox.addCommand(get_exitCommand());
helloTextBox.setCommandListener(this);
}
return helloTextBox;
}

private javax.microedition.lcdui.Command get_exitCommand() {
if (exitCommand == null) {
exitCommand = new javax.microedition.lcdui.Command("Exit", javax.microedition.lcdui.Command.EXIT,
1);
}
return exitCommand;
}

javax.microedition.lcdui.TextBox helloTextBox;
javax.microedition.lcdui.Command exitCommand;


12 - Add a line initialize(); to the startApp() method, so it looks like the following:

public void startApp() {
initialize();
}

13 - Double click the get_helloTextBox() on the Navigator Window to involve_helloTextBox(). Replace the "test string"
code with the text of your choice. For example, "Hello World 2"

14 -
Choose Run > Run Main Project (F6) from the Run menu.

That's: