android Android and Eclipse – J2ME and NetBeans (Part 2)

Posted Jan 3, 2008 by kihbord under Java Mobile Development Software,, , , ,

Comments
Related Posts

How does Android in Eclipse compare with J2ME in NetBeans? I kinda got curious about J2ME and Android source code so I tried creating a simple program and compare the resulting source code.

In my comparison, I have used the NetBeans IDE for creating the source code for J2ME since it’s very easy to setup J2ME development in NetBeans — in fact you don’t need to do anything to setup, just download NetBeans v6.0, install and you’re good to go ;-)

For Google’s open mobile platform — Android, I used Eclipse for obvious reasons. The Android development plugin is only available for Eclipse. It would be cool to see a plugin for NetBeans that takes advantage of tools for Game Development, Visual Design and Program Flow on the NetBeans IDE.

I decided to create something very simple, the ubiquitous “Hello World” program.

Creating in “Hello World” in NetBeans

Creating a “Hello World” J2ME program on NetBeans is a very simple (in fact, I don’t think it can get any simpler). Just a couple of steps.

  1. Once you get NetBeans v6.0 started, Click on File | New Project.Step 1 - Choose NetBeans Project Type
  2. Click next, type the name of the project (HelloMidlet) and click the checkbox “Create Hello Midlet”. The succeeding dialogs are optional. That’s it, NetBeans will create the “Hello World” midlet for you.Step 2 - Enter project name
  3. The generated source code contains the basic structure of a Java midlet, comments and things needed to make J2ME development under NetBeans easier. Here’s a copy of the J2ME program created in NetBeans:

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package hello;import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;

    /**
    * @author kihbord
    */
    public class HelloMIDlet extends MIDlet implements CommandListener {

    private boolean midletPaused = false;

    //<editor-fold defaultstate=”collapsed” desc=” Generated Fields “>
    private Command exitCommand;
    private Form form;
    private StringItem stringItem;
    //</editor-fold>

    /**
    * The HelloMIDlet constructor.
    */
    public HelloMIDlet() {
    }

    //<editor-fold defaultstate=”collapsed” desc=” Generated Methods “>
    //</editor-fold>

    //<editor-fold defaultstate=”collapsed” desc=” Generated Method: initialize “>
    /**
    * Initilizes the application.
    * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
    */
    private void initialize() {
    // write pre-initialize user code here

    // write post-initialize user code here
    }
    //</editor-fold>

    //<editor-fold defaultstate=”collapsed” desc=” Generated Method: startMIDlet “>
    /**
    * Performs an action assigned to the Mobile Device – MIDlet Started point.
    */
    public void startMIDlet() {
    // write pre-action user code here
    switchDisplayable(null, getForm());
    // write post-action user code here
    }
    //</editor-fold>

    //<editor-fold defaultstate=”collapsed” desc=” Generated Method: resumeMIDlet “>
    /**
    * Performs an action assigned to the Mobile Device – MIDlet Resumed point.
    */
    public void resumeMIDlet() {
    // write pre-action user code here

    // write post-action user code here
    }
    //</editor-fold>

    //<editor-fold defaultstate=”collapsed” desc=” Generated Method: switchDisplayable “>
    /**
    * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
    * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
    * @param nextDisplayable the Displayable to be set
    */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
    // write pre-switch user code here
    Display display = getDisplay();
    if (alert == null) {
    display.setCurrent(nextDisplayable);
    } else {
    display.setCurrent(alert, nextDisplayable);
    }
    // write post-switch user code here
    }
    //</editor-fold>

    //<editor-fold defaultstate=”collapsed” desc=” Generated Method: commandAction for Displayables “>
    /**
    * Called by a system to indicated that a command has been invoked on a particular displayable.
    * @param command the Command that was invoked
    * @param displayable the Displayable where the command was invoked
    */
    public void commandAction(Command command, Displayable displayable) {
    // write pre-action user code here
    if (displayable == form) {
    if (command == exitCommand) {
    // write pre-action user code here
    exitMIDlet();
    // write post-action user code here
    }
    }
    // write post-action user code here
    }
    //</editor-fold>

    //<editor-fold defaultstate=”collapsed” desc=” Generated Getter: exitCommand “>
    /**
    * Returns an initiliazed instance of exitCommand component.
    * @return the initialized component instance
    */
    public Command getExitCommand() {
    if (exitCommand == null) {
    // write pre-init user code here
    exitCommand = new Command(“Exit”, Command.EXIT, 0);
    // write post-init user code here
    }
    return exitCommand;
    }
    //</editor-fold>

    //<editor-fold defaultstate=”collapsed” desc=” Generated Getter: form “>
    /**
    * Returns an initiliazed instance of form component.
    * @return the initialized component instance
    */
    public Form getForm() {
    if (form == null) {
    // write pre-init user code here
    form = new Form(“Welcome”, new Item[] { getStringItem() });
    form.addCommand(getExitCommand());
    form.setCommandListener(this);
    // write post-init user code here
    }
    return form;
    }
    //</editor-fold>

    //<editor-fold defaultstate=”collapsed” desc=” Generated Getter: stringItem “>
    /**
    * Returns an initiliazed instance of stringItem component.
    * @return the initialized component instance
    */
    public StringItem getStringItem() {
    if (stringItem == null) {
    // write pre-init user code here
    stringItem = new StringItem(“Hello”, “Hello, World!”);
    // write post-init user code here
    }
    return stringItem;
    }
    //</editor-fold>

    /**
    * Returns a display instance.
    * @return the display instance.
    */
    public Display getDisplay () {
    return Display.getDisplay(this);
    }

    /**
    * Exits MIDlet.
    */
    public void exitMIDlet() {
    switchDisplayable (null, null);
    destroyApp(true);
    notifyDestroyed();
    }

    /**
    * Called when MIDlet is started.
    * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
    */
    public void startApp() {
    if (midletPaused) {
    resumeMIDlet ();
    } else {
    initialize ();
    startMIDlet ();
    }
    midletPaused = false;
    }

    /**
    * Called when MIDlet is paused.
    */
    public void pauseApp() {
    midletPaused = true;
    }

    /**
    * Called to signal the MIDlet to terminate.
    * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
    */
    public void destroyApp(boolean unconditional) {
    }

    }

I’m stopping here for now. Next stop, I’ll put in the Android code generated in Eclipse and my observations.

I’m thinking of writing my very first Android and J2ME software. Something that I can use now and maybe later when an Android cell phone becomes available. Have any suggestions? Maybe a useful utility or a mobile game?

Related posts

Leave a Reply