Implementing High-Level User interface in MIDP

March 7th, 2008 by uCertify Leave a reply »

Developing a user interactive application is a daunting task. A developer needs to be very careful and understanding while developing an application that supports a user interface. He should very intelligently use colors in the application, so that it does not pinch in the eyes of users or appear as overdone. He should be careful while using the user interface components such as text box, check box, etc. For example, if only one of the two options must be selected then instead of using check box, radio buttons should be used.

The profile provides user interface in a mobile application. The CLDC based applications use MIDP, i.e., the Mobile Information Device Profile for providing user interface in the application.

The javax.microedition.lcdui package contains classes for providing user interface in an application. This package must be imported while developing the application.

The high-level user interface class hierarchy is depicted by the following image:

Displayable

The Displayable class is the base class of all user interface classes. The direct subclasses of the Displayable class are Screen and Canvas.

The setCurrent() method of the Displayable class is used to make the Displayable object available on the screen. This method takes the instance of the Displayable class as parameter.

The Screen class has the following subclasses. The user interface is managed by its subclasses. It does not provide any functionalities on its own.

  • Form
  • List
  • Alert
  • TextBox

Each of these subclasses is discussed below in detail:

Form

The Form class is like a placeholder. It covers the entire displayable screen area on the device.
The code below creates a form with a label:

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

public class Tree extends MIDlet {
public StringItem s1;

private Form a; // Form variable
private Display display; //Display variable
public Tree() {
display=Display.getDisplay(this);
//invoke getDisplay() method
a=new Form(”My”);// Form instance
}
public void startApp(){
display.setCurrent(a);
//makes the form visible on the screen
}
public void pauseApp() {
}
public void destroyApp(boolean b) {
}
}

The following items can be placed on a Form:

  • ChoiceGroup
  • Gauge
  • Ticker
  • TextField
  • StringItem

Each item is discussed below in detail:

ChoiceGroup

The ChoiceGroup class is used to create check boxes and radio buttons. An instance of the ChoiceGroup class can either be exclusive or multiple. The exclusive instance is for radio buttons, and the multiple is for check boxes. The ChoiceGroup instance is created with the following syntax:

ChoiceGroup c= new ChoiceGroup(String label, int choicetype)

Here, the label parameter specifies the caption for the check boxes.

The choicetype parameter specifies one of the following three constants:

  • EXCLUSIVE: It allows a user to choose only one option.
  • MULTIPLE: It allows a user to choose more than one option.
  • POPUP: It allows a user to select one item at a time.

The figure below shows a ChoiceGroup with MULTIPLE constant type:

The figure below shows a ChoiceGroup with POPUP constant type:

Gauge

The Gauge class is a subclass of the Item class. It defines methods to create animated progress bar. This progress bar graphically depicts the status of a process. The following syntax is used to create an instance of the Gauge class:

Gauge gauge= new Gauge (String Label, Boolean Val, int Max, int Min)

Here, the Label parameter defines the caption displayed on the screen with the gauge.
The Val parameter defines whether or not the gauge is interactive. A boolean true indicates that the gauge is interactive, and a boolean false indicates that the gauge is non interactive.

The Max parameter defines the maximum value of the gauge.

The Min parameter defines the minimum value of the gauge.

The image below depicts a non-interactive Gauge:

Ticker

Ticker is a class used to display a message across the horizontal screen of the device. An instance of the Ticker class can be associated with any class derived from the Screen class. It has the following methods:

  • Ticker(String str): It creates a new Ticker. The String parameter defines the string to be displayed.
  • String getString(): It extracts the text displayed by the Ticker.
  • void setString(String str): It defines the method to be displayed by the Ticker or replaces the string of an already created Ticker.

TextField

The TextField class is used to take single-line or multiple-line input from a user. The number of characters depends upon the maximum size of the TextField. The general syntax to create a TextField is as follows:

TextField textfield=new TextField(”label”, “input”, “size”, “constraint”)

The label defines the visual text displayed before the text box.

The input is the text entered by a user.

The size defines the maximum number of characters the TextField can take as input.

The constraint defines the TextField constants. These constants are listed below:

  • CONSTRAINT_MASK: It determines the constraint’s current value.
  • ANY: It takes any character as input.
  • EMAILADDR: It takes only a valid email address.
  • NUMERIC: It takes both positive and negative numbers.
  • PASSWORD: It hides the input while a user is entering data.
  • PHONENUMBER: It takes only valid phone numbers.
  • URL: It takes the valid address of a Web site.

StringItem

StringItem is a high-level user interface component. It is used to display a string or pair of strings on a Form. A user cannot modify the value displayed by a StringItem. It defines the following constructor:

public StringItem(String Label, String Text)

Here, the Label parameter defines the caption to be displayed with the StringItem instance.

The Text parameter defines the string to be displayed with the StringItem instance.

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

public class Tree extends MIDlet {
public StringItem s1;

private Form a;
private Display display;
public Tree() {
display=Display.getDisplay(this);
a=new Form(”My”);
s1=new StringItem(”My”, “First Trip to Chicago”);
a.append(s1);
}
public void startApp(){
display.setCurrent(a);
}
public void pauseApp() {
}
public void destroyApp(boolean b) {
}
}

The image below depicts the StringItem created using the above code:

The append() or insert() method is used to associate a Displayable instance with an Item.

List

The List class is a subclass of the Screen class. Like any other subclass of the Screen class, it, too, covers the entire displayable area of the device. It provides user interface by displaying a list of items to a user. The user can select an item from the list. The general syntax for creating a List object is as follows:

List l=new List(java.lang.String title, int listType)

Here, the title parameter specifies the text to be displayed with an instance of the List class.

The listType parameter defines the type of list. It can be one of these types:

  • EXCLUSIVE: It allows a user to select only one item at a time.
  • IMPLICIT: It allows a user to select the currently focused item at the time of a command invocation.
  • MULTIPLE: It allows a user to select more than one item at a time.

Note: A List object does not support POPUP type. An attempt to use it with the List will throw an IllegalArgumentException.

Alert

The Alert class covers the entire screen. It provides user interface. The main function of the Alert class is to inform a user about any errors or exceptions. It has the following two constructors:

Alert(String Title): It creates an empty Alert instance with a specified label or caption.

Alert(String Title, String Alert text, Image Alert type, AlertType A): It creates an Alert instance with the following parameters:

  • Title: It specifies the label or caption for an instance of the Alert class.
  • Alert text: It displays an alert message.
  • Alert type: It specifies an image to be displayed with the specified message. It is an optional parameter.
  • A: It specifies one of the AlertType constants such as WARNING, INFO, etc.

The instance of the Alert class is made available to a user by passing it as an argument to the setCurrent() method.

TextBox

The TextBox like any other subclass of the Screen class covers the entire screen of the device. It is used to take input from a user. It allows multi-line input. It has the following constructor:

TextBox(String label, String text, int size, int type)

The label parameter defines the caption displayed before the TextBox.

The text is the user’s input.

The size defines the number of acceptable characters in the TextBox.

The type parameter defines the kind of value accepted by the TextBox. It can be one of the following:

  • CONSTRAINT_MASK: It determines the constraint’s current value.
  • ANY: It takes any character as input.
  • EMAILADDR: It takes only a valid email address.
  • NUMERIC: It takes both positive and negative numbers.
  • PASSWORD: It hides the input while a user is entering data.
  • PHONENUMBER: It takes only valid phone numbers.
  • URL: It takes the valid address of a Web site.

Display class

The Display class represents a logical device screen on which a MIDlet displays its user interface components such as Form, List, etc. Every MIDlet can access a single instance of the Display class using the getDisplay() method.

The high-level user interface display is simple to implement in an application. It does not allow developers to manipulate the appearance of the above-mentioned components on the screen, i.e., it does not provide a pixel-by-pixel control of the screen like low-level user interface.

Become SUN SCMAD certified. Download practice question and study guide for CX310-110 for exam.
Like this article? Share it with others
If you like this article, please leave a comment or subscribe this blog via RSS or via e-mail, Bookmark and share through your network. Click the AddThis button below. Thanks.
  • Share/Bookmark
Advertisement

Leave a Reply

uCertify.com | Our Company | Articles | Contact Us | News and Press Release | uCertify India | Entries (RSS)
MCSE: MCSA, MCTS, MCITP    JAVA Certification: SCJP, SCWCD    Cisco Certification: CCNA, CCENT    A+, Network+, Security+ Project+
Oracle Certification: OCP 11g, OCP 10g, OCA 11g, OCA 10g    CIW foundation    EC-212-32,    CISSP    Photoshop ACE CS4    Adobe Flash ACE, PMP, CAPM
© 2008 uCertify.com. All rights reserved. All trademarks are the property of their respective owners.