Saturday, February 27, 2010

J2ME - How to write your first app.

After setting up your development environment, now is the time to write our first app in J2ME. We call it hello-mobile. To write your first app, make sure your development environment has been setup properly. Click here to read "How to ensure your J2ME development environment is setup properly".

There are quite a few ways of developing your first J2ME application. We will discuss two ways of doing that.
  1. You can either use Eclipse as your development environment
  2. Or you can use Sun's Micro Edition software development kit

We will mostly use Eclipse for our development tutorials but for the first app, we will demonstrate the both tools. Here we go:

1. Your first J2ME app using Java Platfrom Micro Edition SDK Tool
2. Your first J2ME app using Eclipse


Our tutorials here assume that you have the basic software development knowledge in Java Programming language or have at least acquintance of the language.

Some books for you to learn Java in case you are not very proficient with Java

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


Related Articles

How to ensure your J2ME Development environment is setup properly

J2ME - Basic Introduction

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

Friday, February 26, 2010

How to ensure that J2ME development Environment is setup properly?

In our last post, we discussed in detail, how to setup J2ME development environment on Microsoft Windows. Usually, this setup procedure is quite simple but we still need to make sure that things are configured properly. This post tells you how to ensure that the environment that you have set up is working propserly or not.

Simply launch Java Platform Micro Edition SDK

Click on any of the demo applications listed on the welcome page

Lets say, we clicked on AGUIJava2DDemo and the a pplication was opened and launched. Sun's J2ME emulator loads up and shows the running applicaiton.


In early versions, the SDK used to be called J2ME WTK (J2ME Wireless toolkit) but now it is known as Java Platform Micro Edition SDK.

If the demo loads normally, and runs on the emulator, this means that your development environment has been 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.

J2ME - Basic Introduction

Java is one of the most popular software development languages of current times. The languages is supported not only on conventional computing devices but it also runs embedded on equipment like TV top boxes, microwave ovens etc.

Small devices like hand-held computers and mobile phones were also the obvious platforms for Java apps to run on. J2ME i.e. Java 2 Micro edition is the name of Java edition that runs on these hand held devices. Mobile phones and hand-held computers are built in a vast veriety of configuration, hardware, operating system, screen sizes, IO capabilities and communication capabilities. J2ME tries to work on most of such devices.

To start developing such applications, you should understand the basics of the platform and how things work under the software that you develop for the mobile phones and apps.

Friday, February 19, 2010

Welcome to mobile-apps-dev.com

Welcome to the mobile apps development blog. We will be publishing articles related to mobile application development on different popular software development platforms for the mobile devices. You can use this place as your learning course work while you wade through thick and thin of the mobile apps developement on your favorite platform. We will not confine to one platform and will continue to talk about development on Android, iPhone, BlackBerry, J2ME and other mobile platforms.