J2ME: User Interface

9/3/2010 8:18:25 PM

This section demonstrates how to create various GUI components. The MIDlet in the example shown here is used to test lists, forms, choices, gauges, text fields, and text boxes.


The method startApp( ) starts the MIDlet by creating a list of items and associating the exit command with it.

The following list gives a line-by-line analysis of the program shown in Figure 11.7.

  • List menu ;

The class javax.microedition.lcdui.List is a Screen containing a list of choices.

  • menu = new List( "Test Components", Choice.IMPLICIT );

The constructor public javax.microedition.lcdui.List.List(String title, int listType ) creates a new, empty List with the following two parameters:

  • title — the screen's title, and

  • listType — one of the following three field values:

    • public static final int IMPLICIT : This selects the currently highlighted item when a Command is initiated.

    • pt public static final int EXCLUSIVE: This specifies that only one element can be selected at a time.

    • public static final int MULTIPLE: This allows an arbitrary number of elements to be selected simultaneously.

  • menu.append( "Test TextBox", null );

The method public int javax.microedition.lcdui.List.append( String stringPart, Image imagePart ) appends an element to the Choice , which is an interface of the package javax.microedition.lcdui and defines an API for a user interface components implementing a selection from a predefined number of choices. The added element will be the last element of the Choice .

Figure 11.7. The script

Figure 11.8. The method startApp( )

Figure 11.9. The first two interfaces from the project GuiSuite

  • Ticker ticker = new Ticker( "Test GUI Components" );

The class javax.microedition.lcdui.Ticker implements a "ticker-tape," a piece of text that runs continuously across the display. The direction and speed of scrolling are determined by the implementation. While animated, the ticker string scrolls continuously. The constructor public Ticker( String str ) constructs a new Ticker object, given its initial contents string.

  • menu.setTicker(ticker) ;

The method public void javax.microedition.lcdui.Screen.setTicker ( Ticker ticker ) sets a ticker for use with this Screen , replacing any previous ticker.

  • Gauge gauge = new Gauge( "Progress Bar", false, 20, 9 );

The class javax.microedition.lcdui.Gauge implements a bar graph display of a value intended for use in a form. The constructor public Gauge( String label, boolean interactive, int maxValue, int initialValue ) creates a new Gauge object with the following parameters:

  • label : the Gauge 's label,

  • interactive : interactive or non-interactive mode. In interactive mode, the user is allowed to modify the value.

  • maxValue : the maximum value must be greater than zero.

  • initialValue : the initial value must be within the range zero to maxValue , inclusive.

For example, consider a Gauge object that has a range of values from zero to 99, running on a device that displays the Gauge 's approximate value using a set of one to ten bars. The device might show one bar for values zero through nine, two bars for values ten through 19, three bars for values 20 through 29, and so forth.

  • TextField textfield = new TextField( "TextField Label", "abc", 50, 0 );

The class javax.microedition.lcdui.TextField is an editable text component that may be placed into a Form . The constructor public TextField( String label, String text, int maxSize, int constraints ) creates a new TextField object with the following parameters:

  • label : the item label.

  • text : the initial contents, or null if the TextField is to be empty.

  • maxSize : the maximum capacity in characters.

  • constraints : constant 0 allows users to enter any text. Other values can be found from the Field Summary of input constraints.

  • Form form = new Form("Form for Stuff");

The class javax.microedition.lcdui.Form is a Screen that contains an arbitrary mixture of items: images, read-only text fields, editable text fields, editable date fields, gauges, and choice groups. The constructor public Form( String title ) creates a new, empty Form .

  • form.append(gauge) ;

    form.append(textfield) ;

The method public int javax.microedition.lcdui.Form.append(Item item) adds an Item into the Form . An Item is a superclass for components that can be added to a and Alert . Form

  • DateField date = new DateField( "Today's date:", DateField.DATE );

The class javax.microedition.lcdui.DateField is an editable component for presenting date and time (calendar) information that may be placed into a Form . The constructor public DateField(String label, int mode) creates a DateField object with the following parameters:

  • label : the item label.

  • mode : the input mode, one of the following three modes:

    • DATE : allows only date information to be set.

    • TIME : allows only time information (hours, minutes) to be set.

    • DATE _TIME : allows both clock time and date values to be set.

  • List down = (List) display.getCurrent( );switch (down.getSelectedIndex( )) {

The method public int getSelectedIndex( ) returns the index number of an element in Choice that has been selected. For Choice types EXCLUSIVE and IMPLICIT there is at most one element selected, so this method is useful for determining the user's selection.

Other Interfaces

Figure 11.10 shows screenshots of other interfaces from the project GuiSuite .

  • final Alert soundAlert = new Alert( "sound Alert" );

The class javax.microedition.lcdui.Alert is a screen that shows data to the user and waits for a certain period of time before proceeding to the next screen. An alert is an ordinary screen that can contain text and image, and which handles events like other screens. The constructor public Alert( String title ) constructs a new, empty object with the given title. Alert

  • soundAlert.setType( AlertType.ERROR );

The method public void setType( AlertType type ) sets the type of the Alert . The handling and behavior of specific AlertTypes is described in AlertType . An is designed to alert the user to an erroneous operation. ERROR AlertType

  • soundAlert.setString ( "** ERROR ** " );

The method public void setString( String str ) sets the text string used in the Alert .

  • java.util.Date now = new java.util.Date( );

The class java.util.Date represents a specific instant in time, with millisecond precision. The constructor public Date( ) allocates a Date object and initializes it to represent the current time specified number of milliseconds since the standard base time known as "the epoch," namely January 1, 1970, 00:00:00 GMT.

Figure 11.10. Screenshots of other interfaces from GuiSuite

  • date.setDate( now );

The method public void javax.microedition.lcdui.DateField.setDate( Date date ) sets a new value for this field.

Top 10
Windows Server 2003 : Maintaining, Monitoring, and Troubleshooting Printers
Windows Server 2003 : Advanced Printer Configuration and Management
Windows Server 2003 : Installing and Configuring Printers
Windows 7 : Sharing Resources on a Network - Turn on Sharing and Discovery
Windows 7 : Sharing Resources on a Network - Methods for Sharing in Windows 7
HP Network Node Manager 9 : Before we Manage with NNMi (part 5) - Installing software
HP Network Node Manager 9 : Before we Manage with NNMi (part 4) - Server sizing considerations, Licensing policy
HP Network Node Manager 9 : Before we Manage with NNMi (part 3) - Understanding Smart Plug-ins - iSPI Network Engineering toolset, iSPI IP Telephony, iSPI for MPLS, iSPI multicast
HP Network Node Manager 9 : Before we Manage with NNMi (part 2) - Understanding Smart Plug-ins - iSPI Performance for Metrics
HP Network Node Manager 9 : Before we Manage with NNMi (part 1) - What can HP SW NNMi do for us?
Most View
WD's My Passport (2TB) - Never leave home without it
The best of the web (Part 2) - Evernote & ChallengePost
The Effect Of IOS And Facebook On Shutterbugs (Part 1)
Sharepoint 2007: Upload a File Using the Explorer View
Windows Vista : Performing Local PC Administration (part 1) - Working with workstation administration tools
Windows XP : Verifying Digitally Signed Files, Reviewing Event Viewer Logs, Setting Up a 10-Step Maintenance Schedule
Personalizing Windows 7 (part 2) - Choosing Your Desktop Background
.NET Enterprise Services Technologies : Application Blocks and Software Factories
Fujifilm FinePix F800EXR Super-Zoom Compact With WiFi Connection
Windows Azure : Storing static reference data with dynamic data
Lenovo ThinkPad X220
Windows 7 : Recovering After a Crash or Other Problem (part 3)
Microsoft Wedge And Sculpt Kits For Windows 8
Windows 7 : Using Windows Defender (part 1) - Configuring Windows Defender
Microsoft XNA Game Studio 3.0 : Getting the Date and Time
Windows Azure : Messaging with the queue - Working with basic queue operations
Audioengine A5+ Powered Speakers
Registering CLR Assemblies for Use with SQL Server
iPhone Application Developmen : Using the View-Based Application Template (part 3)
Use the new BBC website (Part 1)