Saturday, July 25, 2009

Implementing CommandListener Interface in J2ME Applications

Classes that implement CommandListener interface, which has a single method: commandAction(Command com, Displayable dis), have the abilitiy to trace command click actions. In order to transfer command information to a listener, the listener is registered with the method setCommandListener(CommandListener listener). In the sample code at below, the command listener in this case is the form class itself which is implementing the commandAction() method.

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;

public class ConfirmationPopUpForm extends Form implements CommandListener {
private Command cmdPreviousPage;
private Command cmdExit;
public ConfirmationPopUpForm(){
this("Confirmation Form");
}
public ConfirmationPopUpForm(String title){
super(title);
cmdPreviousPage = new Command("Return",Command.CANCEL, 0);
addCommand(cmdPreviousPage);
cmdExit = new Command("Exit",Command.OK, 1);
addCommand(cmdExit);
this.setCommandListener(this);
}
public void commandAction(Command cmd, Displayable display) {
if(cmd == cmdExit){
exitAction();
}
else if(cmd == cmdPreviousPage){
showPreviousFormAction();
}
}
private void showPreviousFormAction() {
MainMenuForm.show();
}
private void exitAction() {
StarterForm.show();
}
}

Thursday, July 23, 2009

Unicode Characters Corresponding to Turkish Letters

In order to use Turkish letters ç,ğ,ı,ş,ö,ü,Ç,Ğ,İ,Ş,Ö,Ü in applications, their corresponding unicode character codes can be helpful.

Turkish Unicode Characters
ç --> \u00E7
ğ --> \u011F
ı --> \u0131
ş --> \u015F
ö --> \u00F6
ü --> \u00FC
Ç --> \u00C7
Ğ --> \u011E
İ --> \u0130
Ş --> \u015E
Ö --> \u00D6
Ü --> \u00DC

Wednesday, July 22, 2009

Creating Buttons on J2ME Forms

StringItem can be used in order to create buttons on J2ME forms. Define a new Command variable in your Form and set it as defaultCommand for StringItem variable.

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.StringItem;

public class FormWithButton extends Form implements ItemCommandListener{

private StringItem btnLogin;
private Command cmdLogin;
public FormWithButton(String str) {
super(str);
btnLogin = new StringItem(null, "Login");
cmdLogin = new Command("", Command.OK, 1);
btnLogin.setDefaultCommand(cmdLogin);
btnLogin.setItemCommandListener(this);
append(btnLogin);
}
public void commandAction(Command cmd, Item item) {
LoginForm.show();
}
}

Thursday, July 16, 2009

Singleton Java ME Forms

Some of the mobile forms contain only static elements, which are not interacting with the user. That kind of forms are usually aimed to provide a list of command items on them, which are redirecting users to other forms. So, it is reasonable to design that classess in singleton pattern. Aim of creating a form in singleton pattern is to ensure that a class has only one instance at a time. Instead of creating an instance of that kind of mobile forms each time, you can hide the constructor from global access and make it protected.
The instance that will represent the class is placed as a static field in the class and initialized when it is not created before.


import javax.microedition.lcdui.Form;

public class SingletonForm extends Form {

private static SingletonForm instance;

public static SingletonForm createNew() {
if(instance == null){
instance = new SingletonForm("");
}
return instance;
}

public void showNew() {
MainMidlet.getInstance().getDisplay().setCurrent(this);
}

protected SingletonForm(String str){
super(str);
append("SingletonForm!");
}
}


This is the 'not-thread-safe' version of singleton pattern. If you want it to be thread-safe then use locking. In order to call an instance of the above class you can type in your caller:
SingletonForm.createNew().showNew();