Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Sunday, March 14, 2010

J2ME - Form - Code Example

This example will show you how simple it is to make J2ME Forms. All you have to do is create a MIDlet and in the startApp() method instantiate a Form and show a "Hello" string on top of it. If you have the development environment properly set up, this should take a few moments.

As you have read in the previous articles about the MIDlet life-cycle, startApp() is the method that is called by the J2ME runtime on the device and you can write your initializing code in this MIDlet method.
 
Following is a simple code chunk that will show a Form with a String on it.

How to create a Form in J2ME

In this code, on line 1 we create an instance of the Form. "Form Title" is obviously the text that will show as the title of the form on the screen. On line 2, we append a string to be shown on the form and the last line is making the Form visible on the screen.

        Form f = new Form("Form Title");
        f.append("Hello - This is the form");
        Display.getDisplay(this).setCurrent(f);


Here is the full code MIDlet Example


import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class FormExample extends MIDlet {

    protected void startApp() throws MIDletStateChangeException {
        Form f = new Form("Form Title");
        f.append("Hello - This is the form");
        Display.getDisplay(this).setCurrent(f);
    }
   
    public FormExample() {   
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
    }

    protected void pauseApp() {
    }
}




Output of the above code:







J2ME Form code example








Related Articles:



J2ME - Screen and visual user interfaces

J2ME provides a variety of methods of showing objects and  controls on screen. You can use typical form like screens that will have text boxes, drop downs and buttons for takeing input from user or you can use custom drawing capable panels called Canvas.

Following diagram shows a typical hierarchy of the J2ME user interface classes:

As you can see, every visual element shown on J2ME screens is a sub-class of Displayable. All these classes are found inside javax.microedition.lcdui package or its sub-packages. Displayable hence is also found at javax.microedition.lcdui.Displayable. Screen class is hardly ever used for direct screen display and the most common screen item on J2ME apps is either Form or a Canvas.

Form is a screen that can display other controls like buttons, text fields and check boxes on top of it. Canvas is a more general drawing  board on which you can create visual interfaces by drawing lines, arcs, text, colors and images.

At times, there is no need of a full fledged Form and a full-screen TextBox or a List could do the trick as shown in the hierarchy. Alerts are a way of showing infromation or errors to the user. We will study further about these classes in the later articles.

Related Articles:

J2ME - Form Example
J2ME - Canvas Example
J2ME - List Example
J2ME - Alert Example
J2ME - TextBox Example

Monday, March 8, 2010

J2ME - MIDlet lifecycle explained

MIDlet is the basic execution unit in J2ME and it is in different states in different point of its execution. When you launch an application, operating system loads it up. There are three basic states of a MIDlet:
  1. PAUSED
  2. DESTOYED
  3. ACTIVE
The names are self explanatory. When the application initially loads, its in the PAUSED state. When application starts execution, it transitions from PAUSED to ACTIVE state but before going to active state, startApp() method of the MIDlet is called which you must implement in your MIDlet class.

There are several instances where you yourself or the operating system will request your MIDlet to be paused for example in an event of an incoming call. When your application is requested to be paused, it goes from ACTIVE to PAUSED state and before it gets pasued, pauseApp() method is executed. If you want to stop or pause any running threads in your app while it is in paused state, you should write your code in pauseApp() method.

And whenever you exit your application, it will transition from the ACTIVE or PAUSED state to a DESTROYED state. In destroyed state, all resources are de-allocated and the application process is killed. If you want to run some code at the exit time of your applicatin, like closing network connections and storage devices, you should write your code in destroyApp() method of your MIDlet class.

Following diagram gives a clear picture of the MIDlet lifecycle in J2ME applications.




Related Articles:

What is a MIDlet?
How to write your first J2ME app?
Mobile applications - User interfaces
J2ME - User interfaces

Saturday, February 27, 2010

Your first J2ME app using Java Platform Micro Edition SDK tool



Step 1:  Start Java Platform Micro Edition SDK tool by double clicking its icon or selecting it from the start menu.


Step 2:  From File menu click New-Project.


Step 3:  From "New Project" dialog, click "Java ME SDK" from Categories and click "MIDP Application" from Projects and click Next.


Step 4:  Type "HelloJ2ME" as Project Name, select the project location (leave defaults if not sure).


Step 5:  Click next to see following dialog. Dont change any values but do take a look at the options. We will discuss differences between CLDC and MIDP versions later.


Step 6: Click Finish. Now you see  your new work-place and an automatically generated initial code for you. And you'll see following code out there:





package hello;

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

public class HelloMIDlet extends MIDlet implements CommandListener {

    private Command exitCommand; // The exit command
    private Display display;     // The display for this MIDlet

    public HelloMIDlet() {
        display = Display.getDisplay(this);
        exitCommand = new Command("Exit", Command.EXIT, 0);
    }

    public void startApp() {
        TextBox t = new TextBox("Hello", "Hello, World!", 256, 0);

        t.addCommand(exitCommand);
        t.setCommandListener(this);

        display.setCurrent(t);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable s) {
        if (c == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        }
    }

}


Above code will be generated as a result of above wizard and we will later discuss the details of the code but before that let's run the application to see it in action. Simply ress "F6" to run the application. When you run an app, the IDE launches the J2ME Emulator and following is the output of the program.


J2ME programs, called MIDlets are the main software programs. If you have been programming for Java or similar languages,  you know that the program starts from the main() method but there is no main method in J2ME.

Execution of the program starts from startApp() method. Whenever a MIDlet program is started, its startApp() method is started automatically and the execution starts.

In this program, we have following code generated for you. Here's the explanation:

Create a TextBox control. The body of the textbox will show "Hello World", the title of the box will be "Hello", maximum number of characters that can be netered in this box is 256. Last argument of the consturcotr is the input constraings. The input is not constrained by any rules so the argument is 0. We will read about TextBoxes in more detail later.

TextBox t = new TextBox("Hello", "Hello, World!", 256, 0);


TextBox is a full screen data entry box that we use to display text only. If you want, you can type things in the box. But we do need to add an exit command to the menu of the TextBox so that you can exit the applciation.

        t.addCommand(exitCommand);
        t.setCommandListener(this);



This same class is the command listener, and has to implement the commandAction() method.

        t.setCommandListener(this);

 Finally display the box on the screen.


        display.setCurrent(t);


If this is your first time you are viewing this code and you have any confusions, please do not worry. As we proceed with the tutorials, things will start getting more clear for you.

 ------------------------------------

Related Links:

Start developing J2ME apps using Eclipse

How to setup J2ME development environment

How to make sure J2ME development environment is setup properly

Sunday, February 21, 2010

J2ME - How to setup development environment on Windows

On Microsoft Windows, J2ME development environment requires at least two software to be installed to begin coding J2ME applications.

  1. Java Development Kit.
    You can download the latest version of JDK from Sun's site
  2. Java Platform Micro Edition Software Development Kit 3.0 for Windows
    This can also be downloaded from Sun's website

JDK (Java Development Kit) is required for all sorts of Java development and includes APIs, Documentation and all sorts of tools required to develop java applications for all sorts of development platforms. If you are a Java developer for desktops or servers, you will already be using this tool.