J2ME Contact List
This Example goes to create a Phone Book MIDlet
This example illustrates how to create your phone book. In this example
we are taking three SCREEN button ("Next", "New",
"Exit") and taking two TextBox ("name",
"number"). In the name field user enters name and in number field
user enters phone number. When user enters name and click on Next button then
number text box will open, now user enters phone number. In case user
select "Exit" or "New" the name and number
get prints on the console.
When
program will run initially, startApp() method will be called. And both
"Enter Name" text box and the "Next" button will be
displayed. After that if user clicks on next button the commandAction() method
will be called that will check, if label is equal to "Next" then number TextBox
will appear on the screen that can be used to enter the phone number of
user.
The application display as below:
Source code of PhoneBookExample.java
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class PhoneBookExample extends MIDlet implements CommandListener {
private Command exit, next, New;
private TextBox name, number;
private Display display;
private String nam, no;
private Form form;
public PhoneBookExample(){
next = new Command("Next", Command.SCREEN, 2);
exit = new Command("Exit", Command.SCREEN, 2);
New = new Command("New", Command.SCREEN, 2);
}
public void startApp(){
display = Display.getDisplay(this);
name = new TextBox("Enter Name", "", 30, TextField.ANY);
name.addCommand(next);
name.setCommandListener(this);
number = new TextBox("Enter Number", "", 13, TextField.PHONENUMBER);
number.addCommand(New);
number.addCommand(exit);
number.setCommandListener(this);
display.setCurrent(name);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional){
notifyDestroyed();
}
public void commandAction(Command c, Displayable s){
String label = c.getLabel();
if (label.equals("Exit")){
nam = name.getString();
no = number.getString();
System.out.println("Name = " + name.getString() + ",
Number = "+ number.getString());
destroyApp(false);
} else if (label.equals("Next")){
number.setString("");
display.setCurrent(number);
} else if (label.equals("New")){
display.setCurrent(name);
nam = name.getString();
no = number.getString();
System.out.println("Name = " + name.getString() + ",
Number = "+ number.getString());
name.setString("");
}
}
}
|
Download Source Code