Sunday, January 11, 2009

New Java ME Confirmation Alert

Hello All,
This Java ME alert I've created which might be helpful while creating project with large amount of Forms.

Description : This example consists of one midlet class , one ConfirmationAlert class and one ConfirmAlertListener interface.

Classes and Interfaces Used:
ConfirmAlertListener : as being interface it has two methods (Contract with implementer class)
1) yesPressed -called on positive command press
2) noPressed -called on negative command press

Note: Here positive and negative word means Positive response(like
"Yes","Ok") and Negative response (like "Cancel","Back")
ConfirmationAlert : this class "Delegates" call and behaves as a bridge between
CommandListener of lcdui(i.e. itself) and
ConfirmAlertListener(i.e. in this case HelloWorld ).

HelloMIDlet : this class uses functionality of ConfirmationAlert class and implements
ConfirmAlertListener interface.

Design Principles Applied:
1) seperation of concern
2) program to interface

Design Pattern Used:
1) Observer Pattern

In a project which has many forms whith which user interacts in that case there are frequent ocassions in which you might feel to ask user for confirmation about some action, in that case this code is very helpful to generate "Quickly" an alert with message and two commands(one for positive and another for negative response).
In addition to quickly creating an custom alert this makes design more simple , just create ConfirmAlert and register your class that's it.

Main drawback is as this is for project with more number of forms , it will create two(one class and one interface) components which will take some amount of memory.
So if you are not concerned too much with memory and target devices are having enough memory this will surely help you.
In case where memory is main concern you can surely have "Inner Version" of this class and interface that might help you.


package hello;
* This listener interface is for alert of type
* the class implementing it is notified when
* specific confirmation response is given by user.
* @author Sandeep Gadhvi
public interface ConfirmAlertListener {
* this method is called when "positive" response is given
* to the alert by user.
public void yesPressed();
* this method is called when "negative" response is given
* to the alert by user.
public void noPressed();

package hello;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;

* Confirmation alert to get response from user
* in either positive or negative way.
* @author Sandeep Gadhvi
public class ConfirmationAlert implements CommandListener{

private Alert alert;
private Command positive;
private Command negative;
private ConfirmAlertListener listener;
* constructor for Confirmation alert
* @param msg message to display in alert
* @param positive positive command in confirmation
* @param negative negative command in confirmation
public ConfirmationAlert(String msg,Command positive,Command negative){
alert=new Alert("Confirm Alert",msg,null,AlertType.WARNING);
* registers the listener for confirmation alert
* @param listener listener class implementing ComfirmAlertListener
public void setConfirmAlertListener(ConfirmAlertListener listener){
* get the alert reference for this ConfirmationAlert
*@return alert for this class
public Alert getAlert(){
return alert;
* command action for displayables
* @param command command object reference
* @param display displayable
public void commandAction(Command command, Displayable display) {
// call yes pressed method of ConfirmAlertListener implementer class
// call no pressed method of ConfirmAlertListener implementer class

package hello;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

* This is test midlet class which shows how to use "ConfirmationAlert Class"
* and "ConfirmAlertListener Interface", Its a simple "Hello World" program
* but with Logic of Alert and Listener seperated from main code.Thus encapsulating
* the code.
* @author Sandeep Gadhvi.
public class HelloMIDlet extends MIDlet implements CommandListener,ConfirmAlertListener {

private boolean midletPaused = false;

private Command exitCommand;

private Command yesCommand;
private Command noCommand;
private ConfirmationAlert confirmAlert;
private Alert alert;

private Form form;
private StringItem stringItem;

public HelloMIDlet() {
yesCommand=new Command("Yes",Command.OK,1);
noCommand=new Command("No",Command.CANCEL,1);
// create new ConfirmationAlert object with message to show and
// positive command and negative command for corresponding response.
confirmAlert=new ConfirmationAlert("Do You Really Want To Exit?", yesCommand, noCommand);
//don't forget to registern listener as in every event driven architecture.
// get the custom alert

public void startMIDlet() {
switchDisplayable(null, getForm());
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {

Display display = getDisplay();
if (alert == null) {
} else {
display.setCurrent(alert, nextDisplayable);

public void commandAction(Command command, Displayable displayable) {
if(displayable == form){
if (command == exitCommand) {
// on exit command show "Alert" to user
* This will be called when user presses "Yes" command
public void yesPressed() {
* This will be called when user presses "No" command
public void noPressed() {
public Command getExitCommand() {
if (exitCommand == null) {
exitCommand = new Command("Exit", Command.EXIT, 0);
return exitCommand;
public Form getForm() {
if (form == null) {
form = new Form("Welcome", new Item[] { getStringItem() });
return form;
public StringItem getStringItem() {
if (stringItem == null) {
stringItem = new StringItem("Hello", "Hello, World!");
return stringItem;
public Display getDisplay () {
return Display.getDisplay(this);
public void exitMIDlet() {
switchDisplayable (null, null);
public void startApp() {
if (midletPaused) {
} else {
startMIDlet ();
midletPaused = false;
public void pauseApp() {
midletPaused = true;
public void destroyApp(boolean unconditional) {

Any Suggestions or Comments regarding this article will be appreciated.

Sandeep Gadhvi.

Saturday, January 10, 2009

First Blog

Hi All,
Though this is my first blog ,degree of enthusiasm is not so much higher but still I have much much to write in the future.
For those first time visiting my blog I am sandeep gadhvi , from ahmedabad (basically from gandhidham,kutch) doing job at one middle scale IT company at ahmedabad.
For those who are familiar with me you know i don't have anything to write that's why i am filling up this empty space.
So......... Enough is Enough .......This is me you like or not.

We'll meet in future with lots of text pouring out of this blog and your brain pouring out of your head......ha ha ha...

Byee for now "Hasta La Vista".......