Upload
abhijit-borah
View
108
Download
1
Embed Size (px)
Citation preview
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class MidletHelloWorld extends MIDlet {
//reference to the application display manager
private Display display = null;
//TextBox use to print the Hello Word message
private TextBox tbMainForm;
public MidletHelloWorld(){
tbMainForm = new TextBox("My First MIDlet","Hello World !",100,0);
}
public void startApp() {
if(display==null)
display = Display.getDisplay(this);
//activate the form
display.setCurrent(tbMainForm);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
------------------------------------------------------------
//define the MIDlet constructor
public MidletUI()
{
textBox = new TextBox(
"Hello J2ME!", //the text from the form title bar
"Your message is: ", //content
255, //maximum number of characters
TextField.ANY); //filter on the content
}
-------------------------------------------
public void startApp() {
//get the reference for the Display instance
if(midletDisplay==null)
midletDisplay = Display.getDisplay(this);
//display the form
midletDisplay.setCurrent(textBox);
//define an alert
Alert alert = new Alert("Info",
"The MIDlet has started !",
null, AlertType.INFO);
//display the alert and set the TextBox as the next displayable
midletDisplay.setCurrent(alert, textBox);
}
----------------------------------------------------------------------------------
Radio button
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
public class RadioButtons1 extends MIDlet implements ItemStateListener, CommandListener {
private Display display;
private Form form = new Form("");
private Command exit = new Command("Exit", Command.EXIT, 1);
private ChoiceGroup radioButtons = new ChoiceGroup("Select Your Color", Choice.EXCLUSIVE);
private int defaultIndex;
public RadioButtons1() {
display = Display.getDisplay(this);
radioButtons.append("Red", null);
radioButtons.append("White", null);
radioButtons.append("Blue", null);
radioButtons.append("Green", null);
defaultIndex = radioButtons.append("All", null);
radioButtons.setSelectedIndex(defaultIndex, true);
form.addCommand(exit);
form.setCommandListener(this);
form.setItemStateListener(this);
}
public void startApp() {
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command command, Displayable displayable) {
if (command == exit) {
destroyApp(true);
notifyDestroyed();
}
}
public void itemStateChanged(Item item) {
if (item == radioButtons) {
StringItem msg = new StringItem("Your color is ", radioButtons.getString(radioButtons
.getSelectedIndex()));
form.append(msg);
}
}
}
------------------------------------------------
CALCULATOR Program
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.io.*;
public class calculator extends MIDlet implements CommandListener
{
private Form form;
private Display display;
private TextField input1, input2;
private Command add, sub, mul,div;
private StringItem item;
public calculator()
{
}
public void startApp()
{
display = Display.getDisplay(this);
Form form = new Form("Calculator");
form.append("Hello everybody");
item = new StringItem("Result", "");
input1 = new TextField("First Number:", "", 30, TextField.NUMERIC);
input2 = new TextField("Second Number", "", 30, TextField.NUMERIC);
form.append(input1);
form.append(input2);
add = new Command("Addition", Command.OK, 1);
sub = new Command("Subtraction", Command.OK, 1);
mul = new Command("Multiplication", Command.OK, 1);
div = new Command("Division", Command.OK, 1);
form.addCommand(add);
form.addCommand(sub);
form.addCommand(mul);
form.addCommand(div);
form.append(item);
form.setCommandListener(this);
display.setCurrent(form);
}
public void pauseApp() { }
public void destroyApp(boolean uncondn)
{
notifyDestroyed();
}
private void calculate()
{int one=Integer.parseInt(input1.getString());
int two= Integer.parseInt(input2.getString());
int result=one+two;
item.setText( result + "" );
}
private void calculate1()
{
int one = Integer.parseInt(input1.getString());
int two = Integer.parseInt(input2.getString());
int result = one - two;
item.setText(result + "");
}
private void calculate2()
{
int one = Integer.parseInt(input1.getString());
int two = Integer.parseInt(input2.getString());
int result = one * two;
item.setText(result + "");
}
private void calculate3()
{
int one = Integer.parseInt(input1.getString());
int two = Integer.parseInt(input2.getString());
int result = one / two;
item.setText(result + "");
}
public void commandAction(Command c, Displayable d)
{
String label = c.getLabel();
if (label.equals("Addition"))
{
calculate();
}
else if (label.equals("Subtraction"))
{
calculate1();
}
else if (label.equals("Multiplication"))
{
calculate2();
form.append("The Answer is:");
}
else if (label.equals("Division"))
{
calculate3();
form.append("The Answer is:");
}
}
}
-----------------------------------------
This example demonstrates how to create various GUI components. The MIDlet for this example allows you to test lists, forms, choices, gauges, text fields, text boxes, for example.
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class GuiTests extends MIDlet implements CommandListener {
// display manager
Display display = null;
// a menu with items
List menu = null; // main menu
// list of choices
List choose = null;
// textbox
TextBox input = null;
// ticker
Ticker ticker = new Ticker("Test GUI Components");
// alerts
final Alert soundAlert = new Alert("sound Alert");
// date
DateField date = new DateField("Today's date: ", DateField.DATE);
// form
Form form = new Form("Form for Stuff");
// gauge
Gauge gauge = new Gauge("Gauge Label", true, 10, 0);
// text field
TextField textfield = new TextField("TextField Label", "abc", 50, 0);
// command
static final Command backCommand = new Command("Back", Command.BACK, 0);
static final Command mainMenuCommand = new Command("Main", Command.SCREEN, 1);
static final Command exitCommand = new Command("Exit", Command.STOP, 2);
String currentMenu = null;
// constructor.
public GuiTests() {
}
/**
* Start the MIDlet by creating a list of items and associating the
* exit command with it.
*/
public void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
// open a db stock file
menu = new List("Test Components", Choice.IMPLICIT);
menu.append("Test TextBox", null);
menu.append("Test List", null);
menu.append("Test Alert", null);
menu.append("Test Date", null);
menu.append("Test Form", null);
menu.addCommand(exitCommand);
menu.setCommandListener(this);
menu.setTicker(ticker);
mainMenu();
}
public void pauseApp() {
display = null;
choose = null;
menu = null;
ticker = null;
form = null;
input = null;
gauge = null;
textfield = null;
}
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
// main menu
void mainMenu() {
display.setCurrent(menu);
currentMenu = "Main";
}
/**
* Test the TextBox component.
*/
public void testTextBox() {
System.out.println("textbox called");
input = new TextBox("Enter Some Text:", "", 5, TextField.ANY);
input.setTicker(new Ticker("testTextBox"));
input.addCommand(backCommand);
input.setCommandListener(this);
input.setString("");
display.setCurrent(input);
currentMenu = "input";
}
/**
* Test the List component.
*/
public void testList() {
choose = new List("Choose Items", Choice.MULTIPLE);
choose.setTicker(new Ticker("listTest"));
choose.addCommand(backCommand);
choose.setCommandListener(this);
choose.append("Item 1", null);
choose.append("Item 2", null);
choose.append("Item 3", null);
display.setCurrent(choose);
currentMenu = "list";
}
/**
* Test the Alert component.
*/
public void testAlert() {
soundAlert.setType(AlertType.ERROR);
soundAlert.setString("** ERROR **");
display.setCurrent(soundAlert);
}
/**
* Test the DateField component.
*/
public void testDate() {
java.util.Date now = new java.util.Date();
date.setDate(now);
Form f = new Form("Today's date");
f.append(date);
f.addCommand(backCommand);
f.setCommandListener(this);
display.setCurrent(f);
currentMenu = "date";
}
/**
* Test the Form component.
*/
public void testForm() {
form.append(gauge);
form.append(textfield);
form.addCommand(backCommand);
form.setCommandListener(this);
display.setCurrent(form);
currentMenu = "form";
}
/**
* Handle events.
*/
public void commandAction(Command c, Displayable d) {
String label = c.getLabel();
if (label.equals("Exit")) {
destroyApp(true);
} else if (label.equals("Back")) {
if(currentMenu.equals("list") || currentMenu.equals("input") ||
currentMenu.equals("date") || currentMenu.equals("form")) {
// go back to menu
mainMenu();
}
} else {
List down = (List)display.getCurrent();
switch(down.getSelectedIndex()) {
case 0: testTextBox();break;
case 1: testList();break;
case 2: testAlert();break;
case 3: testDate();break;
case 4: testForm();break;
}
}
}
}
------------------------------------------------------------
CREATING FORM COMPONENTS
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class UserRegistrationMIDlet extends MIDlet implements CommandListener {
private Display display;
private Form registrationFrm;
private Alert messageAlert;
private TextField emailTxt;
private TextField passwordTxt;
private TextField nameTxt;
private TextField mobileTxt;
private TextField urlTxt;
private Command okCmd;
private Command exitCmd;
public UserRegistrationMIDlet() {
emailTxt = new TextField("Email:", "", 100, TextField.EMAILADDR);
passwordTxt = new TextField("Password:", "", 16, TextField.PASSWORD);
nameTxt = new TextField("Name:", "", 50, TextField.ANY);
mobileTxt = new TextField("Mobile:", "", 15, TextField.PHONENUMBER);
urlTxt = new TextField("Website:", "", 100, TextField.URL);
registrationFrm = new Form("User Registration",
new Item[] {emailTxt, passwordTxt, nameTxt, mobileTxt, urlTxt});
messageAlert = new Alert("Registration Complete");
messageAlert.setTimeout(5000);
messageAlert.setType(AlertType.CONFIRMATION);
okCmd = new Command("OK", Command.OK, 0);
exitCmd = new Command("Exit", Command.EXIT, 1);
registrationFrm.addCommand(okCmd);
registrationFrm.addCommand(exitCmd);
registrationFrm.setCommandListener(this);
}
protected void startApp() {
display = Display.getDisplay(this);
display.setCurrent(registrationFrm);
}
public void commandAction(Command c, Displayable d) {
if (d == registrationFrm) {
if (c == okCmd) {
String messageContent;
messageContent = "Thanks " + nameTxt.getString() +
"\nRegistration Complete.";
messageAlert.setString(messageContent);
display.setCurrent(messageAlert, registrationFrm);
} else if (c == exitCmd) {
notifyDestroyed();
}
}
}
protected void pauseApp() {
}
protected void destroyApp(boolean unconditional) {
}
}
-------------------------------------------------------------
Checkbox creation
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class CheckExp extends MIDlet implements CommandListener
{
Form form;
Command exit,save;
ChoiceGroup check;
public void startApp()
{
Display display = Display.getDisplay(this);
form = new Form("Check Box Example");
try
{ check = new ChoiceGroup("Subjects : ",Choice.MULTIPLE);
check.append("IP",null);
check.append("ADBMS",null);
check.append("COM",null);
}
catch(Exception e)
{e.printStackTrace();}
form.append(check);
exit = new Command("Exit", Command.EXIT, 1);
save = new Command("Save", Command.OK, 1);
form.addCommand(save);
form.addCommand(exit);
form.setCommandListener(this);
display.setCurrent(form);
}
public void pauseApp () {}
public void destroyApp(boolean unconditional) {}
public void commandAction(Command c, Displayable s)
{
if (c == exit)
{
notifyDestroyed();
}
}
}
-------------------------------------------------
RADIO BUTTON
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class RadioExp extends MIDlet implements CommandListener
{
Form form;
Command exit;
ChoiceGroup radio;
public void startApp()
{
Display display = Display.getDisplay(this);
form = new Form("Radio Button Example");
try
{
radio = new ChoiceGroup("Gender : ",Choice.EXCLUSIVE);
radio.append("Male",null);
radio.append("Female",null);
}
catch(Exception e)
{
e.printStackTrace();
}
form.append(radio);
exit = new Command("Exit", Command.EXIT, 1);
form.addCommand(exit);
form.setCommandListener(this);
display.setCurrent(form);
}
public void pauseApp () {}
public void destroyApp(boolean unconditional) {}
public void commandAction(Command c, Displayable s)
{
if (c == exit)
{
notifyDestroyed();
}
} }
---------------------------------------
Displaying an Image
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class img extends MIDlet
{
private Display display;
private Form fmMain;
public img()
{
display = Display.getDisplay(this);
fmMain = new Form("dis_img");
}
public void startApp()
{
try
{
Image im = Image.createImage("/Sunset.jpg");
fmMain.append(new ImageItem(null, im, ImageItem.LAYOUT_CENTER , null));
display.setCurrent(fmMain);
}
catch (java.io.IOException e)
{
System.err.println("Unable to locate or read .jpg file");
}
}
public void pauseApp(){}
public void destroyApp(boolean unconditional){}
}
---------------------------------------------------------------------------
Ticker program to continuously scroll a data message on the mobile screen.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class TickerExample extends MIDlet implements CommandListener{
private Display display;
private Ticker ticker;
private Command exit;
private Form form;
public TickerExample()
{
form = new Form("Ticker Example");
display = Display.getDisplay(this);
ticker = new Ticker ("This is an Ticker Example");
exit = new Command("Exit", Command.SCREEN, 1);
form.addCommand(exit);
form.setCommandListener(this);
form.setTicker(ticker);
}
public void startApp()
{
display.setCurrent(form);
}
public void pauseApp(){ }
public void destroyApp(boolean unconditional)
{
notifyDestroyed();
}
public void commandAction(Command c, Displayable display)
{
String label = c.getLabel();
if (label.equals("Exit")){
destroyApp(false);
}
}}
---------------------------------------------------------------------
J2ME Program to Read the Contents of a File
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ReadFile extends MIDlet implements CommandListener
{
private Display display;
private Form form;
private Command read, exit;
private Alert alert;
public ReadFile()
{
display = Display.getDisplay(this);
read = new Command("Read", Command.SCREEN, 1);
exit = new Command("Exit", Command.EXIT, 1);
form = new Form("Read File");
form.addCommand(exit);
form.addCommand(read);
form.setCommandListener(this);
}
public void startApp()
{
display.setCurrent(form);
}
public void pauseApp()
{}
public void destroyApp(boolean unconditional)
{
notifyDestroyed();
}
public void commandAction(Command c, Displayable s)
{
String label = c.getLabel();
if (label.equals("Read")){
String string = file();
if (string != null)
{
alert = new Alert("Reading", string, null, null);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert, form);
}
} else if (label.equals("Exit")){
destroyApp(false);
}
}
private String file()
{
InputStream is = getClass().getResourceAsStream("help.txt");
StringBuffer sb = new StringBuffer();
try{
int chars, i = 0;
while ((chars = is.read()) != -1){
sb.append((char) chars);
}
return sb.toString();
}
catch (Exception e)
{}
return null;
}
}
-----------------------------------------------
J2ME Program for Command Button
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class CommandExample extends MIDlet implements CommandListener{
private Form form;
private Display display;
private Command ok, back, cancel, exit, help, item, screen, stop;
public CommandExample(){
form = new Form("Command Form");
screen = new Command("SCREEN", Command.SCREEN, 1);
back = new Command("BACK", Command.BACK, 2);
cancel = new Command("CANCEL", Command.CANCEL, 3);
ok = new Command("OK", Command.OK, 4);
help = new Command("HELP", Command.HELP, 5);
stop = new Command("STOP", Command.STOP, 6);
exit = new Command("EXIT", Command.EXIT, 7);
item = new Command("ITEM", Command.ITEM, 8);
}
public void startApp(){
display = Display.getDisplay(this);
form.addCommand(screen);
form.addCommand(back);
form.addCommand(cancel);
form.addCommand(ok);
form.addCommand(help);
form.addCommand(stop);
form.addCommand(exit);
form.addCommand(item);
form.setCommandListener(this);
display.setCurrent(form);
}
public void pauseApp(){}
public void destroyApp(boolean destroy){
notifyDestroyed();
}
public void backCom(){
Alert back = new Alert("BACK Command",
"Back Command Executed!", null, AlertType.INFO);
back.setTimeout(5000);
display.setCurrent(back, form);
}
public void okCom(){
Alert ok = new Alert("OK Command",
"OK Command Executed!", null, AlertType.INFO);
ok.setTimeout(5000);
display.setCurrent(ok, form);
}
public void cancelCom(){
Alert cancel = new Alert("CANCEL Command",
"Cancel Command Executed!", null, AlertType.INFO);
cancel.setTimeout(5000);
display.setCurrent(cancel, form);
}
public void exitCom(){
Alert exit = new Alert("EXIT Command",
"Exit Command Executed!", null, AlertType.INFO);
exit.setTimeout(5000);
display.setCurrent(exit, form);
}
public void commandAction(Command c, Displayable d)
{
String label = c.getLabel();
if(label.equals("BACK")){
backCom();
} else if(label.equals("OK")){
okCom();
} else if(label.equals("CANCEL")){
cancelCom();
} else if(label.equals("EXIT")){
exitCom();
}
}
}
--------------------------------
J2ME program for ItemStateListener
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class ItemStateListenerMIDlet extends MIDlet{
private Display display;
private Form form;
private Date date = new Date();
private Calendar calendar;
private DateField today;
public ItemStateListenerMIDlet(){
display = Display.getDisplay(this);
}
public void startApp(){
form = new Form("ItemStateListener");
ItemStateListener listener = new ItemStateListener(){
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
public void itemStateChanged(Item item){
calendar.setTime(((DateField)item).getDate());
}
};
form.setItemStateListener(listener);
today = new DateField("Today's Date:", DateField.DATE);
today.setDate(date);
form.append(today);
display.setCurrent(form);
}
public void pauseApp(){
display = null;
}
public void destroyApp(boolean unconditional){
notifyDestroyed();
}
}
--------------------------------------------
J2ME program for EventHandling
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class EventHandlingMIDlet extends MIDlet implements CommandListener {
private Display display;
private List list;
private TextBox input;
private Command back, main, exit;
private String currentItem;
public EventHandlingMIDlet() {
back = new Command("Back", Command.BACK, 0);
main = new Command("Main", Command.SCREEN, 1);
exit = new Command("Exit", Command.STOP, 2);
}
public void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
list = new List("Menu Items", Choice.IMPLICIT);
list.append("EventItem1", null);
list.append("EventItem2", null);
list.append("EventItem3", null);
list.append("EventItem4", null);
list.addCommand(exit);
list.setCommandListener(this);
mainItem();
}
public void pauseApp() {
display = null;
list = null;
input = null;
}
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
void mainItem() {
display.setCurrent(list);
currentItem = "Main";
}
public void show() {
input = new TextBox("Enter Text Here: ", "", 10, TextField.ANY);
input.addCommand(back);
input.setCommandListener(this);
input.setString("");
display.setCurrent(input);
}
public void commandAction(Command c, Displayable d) {
String label = c.getLabel();
if (label.equals("Exit")) {
destroyApp(true);
} else if (label.equals("Back")) {
if(currentItem.equals("Eventitem1") || currentItem.equals("Eventitem2") ||
currentItem.equals("Eventitem3") || currentItem.equals("Eventitem4")){
mainItem();
}
} else {
List down = (List)display.getCurrent();
switch(down.getSelectedIndex()) {
case 0: showItem();break;
case 1: showItem2();break;
case 2: showItem3();break;
case 3: showItem4();break;
}
}
}
public void showItem() {
show();
currentItem = "Eventitem1";
}
public void showItem2() {
show();
currentItem = "Eventitem2";
}
public void showItem3() {
show();
currentItem = "Eventitem3";
}
public void showItem4() {
show();
currentItem = "Eventitem4";
}
}
--------------------------------------------------------------