Search This Blog

Wednesday, 16 August 2023

Exercise 2 of Mobile Application Development Lab

0 comments

Development of Hello World Application

(Experiment 2)

Two packages must be imported at the beginning of the MIDlet to access MIDlet classes and lcdui classes.
  • MIDlet classes are screen oriented and create a Display object and then place components of the screen into the Display object. 
  • The Display object is then invoked later in the MIDlet to display the screen on the small computing device.

HelloWorld MIDlet

  • The HelloWorld MIDlet is created by defining a class called HelloWorld that extends the MIDlet class and implements a CommandListener.
  • The data members are a Display object, a text box, and a command. 
  • The methods are startApp(), pauseApp(), destroyApp() and commandAction().  The method commandAction() is invoked by the application manager whenever an event occurs. 



The HelloWorld MIDlet performs three basic functions that are found in nearly all MIDlets. These are to display a text box and a command on the screen, then listen to events that occur while the MIDlet is running.  

Version 1:

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

public class HelloMidlet extends MIDlet {
private Form form;
private Display display;

public HelloMidlet(){ super(); }

public void startApp() {
     form = new Form("Hello World");
     String msg = "Hello World!!!!!!!";
     form.append(msg);
     display = Display.getDisplay(this);
     display.setCurrent(form);
    }
   
    public void pauseApp() {
    }
   
    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }
}

Output:


Version 2:

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

public class TextBoxMidlet extends MIDlet {

    Display disp = Display.getDisplay(this);
    TextBox text1;
    
    public void startApp() {
        text1 = new TextBox("Hello TextBox","Welcome to J2ME Programming",50,0);
        disp.setCurrent(text1);
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
}

Output:



Version 3:

import javax.microedition.midlet.*; 
import javax.microedition.lcdui.*; 
public class HelloWorld extends MIDlet implements CommandListener 
private Display display; 
private TextBox textBox ; 
private Command quitCommand; 

public void startApp() 
display = Display.getDisplay(this); 
quitCommand = new Command("Quit", Command.SCREEN, 1); 
textBox = new TextBox("Hello World", "My first MIDlet", 40, 0); 
textBox .addCommand(quitCommand); 
textBox .setCommandListener(this); 
display .setCurrent(textBox ); 

public void pauseApp() 
{ } 

public void destroyApp(boolean unconditional) 
{ } 

public void commandAction(Command choice, Displayable displayable) 
if (choice == quitCommand) 
destroyApp(false); 
notifyDestroyed(); 
}

Output:












Version 4:

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

public class HelloMidlet extends MIDlet implements CommandListener{
private Form form;
private TextField txtHello;
private Command cmdDisp;
private Display display;
public StringItem st;

public HelloMidlet(){ super(); }

public void startApp() {
     form = new Form("Hello MIDlet");
     String msg = "Hello World!!!!!!!";
     txtHello = new TextField("Welcome Message", msg, 50, 0);
     cmdDisp = new Command("Display",Command.OK,1);
     form.append(txtHello);
     form.addCommand(cmdDisp);
     form.setCommandListener(this);
     display = Display.getDisplay(this);
     st=new StringItem("",""); 
     display.setCurrent(form);
    }
   
    public void pauseApp() {
    }
   
    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }
    public void commandAction(Command command,Displayable displayable) { 
        if(command==cmdDisp) { 
            st.setText(""); 
            st.setText(txtHello.getString()); 
            form.append(st); 
        }
    }
}

Output:


Leave a Reply