×
Namespaces

Variants
Actions

MIDP's User Interface Hierarchy: Form

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: thiagobrunoms (15 Oct 2009)
Last edited: hamishwillee (01 Aug 2013)

Overview

In our fourth part of the MIDP's User Interface Hierarchy series, we discussed about the Alert MIDP's UI component (MIDP's User Interface Hierarchy: Alert). In this fifth part I'll discuss about another Screen-based component called Form (javax.microedition.lcdui.Form). In addition, since Form provides the mechanisms to show multiple components on the display, I'll also in next posts each of these components, which are subclass os the Item (javax.microedition.lcdui.Item) class.

Form Component

The Form screen is a screen-based component which aims to show a set of small components which are subclass of Item (javax.microedition.lcdui.Item) class. You can append (remove, insert, replace, etc) as many Items as possible, such as calendars (DateField - javax.microedition.lcdui.DateField), text fields (TextField - 'javax.microedition.lcdui.TextField), images (ImageItem - javax.microedition.lcdui.ImageItem), loading bars (Gauge - javax.microedition.lcdui.Gauge), etc. The Form screen can be used, for instance, when you need to create a user registration form for getting his name, address, birthday, and so forth.

The following table shows the main constructors and methods regarding to the Form screen:

TableForm.png

To create your own Form screen, you can only extend the Form class. The described constructors are shown below.

public class MyForm extends Form {
 
/**
* Creates a new Form with a title
* @param title
*/

public MyForm(String title) {
super(title);
}
 
/**
* Creates a new Form with a title and initial set of Item objects
* @param arg0
* @param arg1
*/

public MyForm(String title, Item[] items) {
super(title, items);
}
 
}

To create a new MyForm Form screen, you can do as follows:

Display d       = Display.getDisplay(midletReference); //gets a display reference
MyForm form = new MyForm("My Form"); //creates a new Form
d.setCurrent(form); //sets form to visible

Note in the following figure that no Item objects were appended to the Form ("no data"). If you want to append a new Item, you can use one of the overridden append methods, which were described in table 1.

FormEmpty.jpg

For every new Item that you append, the append method returns a numeric index that represents the Item's position, with an index value starting at 0. Many of the available methods use this index value as an input parameter. As a result, it is important to keep a copy of these values.

For instance, if we want to append a set of "String" objects, we could do:

String s1 = "Thiago"
String s2 = "Sales"
 
int index1 = form.append(s1);
int index2 = form.append(s2);

The following figure shows the two new Strings objects. Note that we kept the place that they have been inserted.

FormWithStrings.jpg

Item

An Item component can be added to a Form. The following architecture shows the available Item components in the MIDP. That is, a Form screen has many Item components:

public class javax.microedition.lcdui.Form extends javax.microedition.lcdui.Screen {
 
private javax.microedition.lcdui.Item[] items;
 
}

ArchitectureForItems.png

The architecture depicted above shows that we can have 6 different types of Item component (ChoiceGroup, DateField, ImageItem, StringItem, TextField and Gauge). Item components can fire events and these events can be handled by an ItemStateChangeListener (javax.microedition.lcdui.ItemStateListener) instance. Once implementing this interface, you should implement the following method:

   public void itemStateChanged(Item arg0)

which is fired by the device's application manager, passing the Item reference that had its state changed. For instance, our first example can be changed to capture the Item state changes:

public class MyForm extends Form implements ItemStateListener {
 
/**
* Creates a new Form with a title
* @param title
*/

public MyForm(String title) {
super(title);
setItemStateListener(this); //sets "this" as the ItemStateChangeListener client
}
 
/**
* Creates a new Form with a title and initial set of Item objects
* @param arg0
* @param arg1
*/

public MyForm(String title, Item[] items) {
super(title, items);
setItemStateListener(this); //sets "this" as the ItemStateChangeListener client
}
 
/*
* //handles any Item state changes.
* arg0 refers to the Item reference that was changed.
* @see javax.microedition.lcdui.ItemStateListener#itemStateChanged(javax.microedition.lcdui.Item)
*/

public void itemStateChanged(Item arg0) {
//process the changes of the given Item reference
}
 
}

Each Item component will be discussed in next posts of the MIDP's User Interface series.

Finally, this fifth part of this series introduced the Form screen-based UI component. The next part I will talk over about the Item's components (MIDP's User Interface Hierarchy: Item Components).

This page was last modified on 1 August 2013, at 09:51.
41 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×