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 characters/letters ç,ğ,ı,ş,ö,ü,Ç,Ğ,İ,Ş,Ö,Ü in applications, their corresponding unicode character codes can be helpful.



Turkish Unicode Characters/Letters :


     ç    -->   \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();
}
}