J2ME Form Class

In this J2ME Extends Form example, we are going to discuss about form, extending form in our class and appending values to it.

J2ME Form Class

J2ME Form Class

     

In this J2ME Extends Form example, we are going to discuss about form, extending form in our class and appending values to it. But first of all lets discuss about form. In general, a form is a screen and this screen can contain any type of items such as images, text and text fields to get or show values to users. 

We can also set values into these forms using following syntax?

public int append(Item item)

The forgoing syntax can be used to add items into form in J2ME.

The application will look like as follow...

J2ME form class, append item example

In this image, "My Form" is a form created by us and "Hello Item" is a item that is appended in this form using append method.

Syntax of J2ME Form Class File name AppendItem.java

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

public class AppendItem extends MIDlet{
  private Display display;
  private MIDlet midlet;

  public void startApp(){
  Form form = new MyForm("MY FORM");
  display = Display.getDisplay(this);
  display.setCurrent(form);
  }

  public void pauseApp(){}

  public void destroyApp(boolean unconditional){}

  class MyForm extends Form implements CommandListener{
  private StringItem textItem;
  private Command exit;
  
  public MyForm(String title){
  super(title);
  exit = new Command("Exit", Command.BACK, 1);
  this.textItem = new StringItem (null, "Hello Item");
  addCommand(exit);
  append(this.textItem);
  this.setCommandListener(this);
  }

  public void commandAction(Command c, Displayable d){
  String label = c.getLabel();
  if(label.equals("Exit")){
  midlet.notifyDestroyed()
  }
  }
  }
}

Download Source Code