Professional Documents
Culture Documents
Canvas. Your MIDlet will assume responsibility for most of the drawing, but you get much finer
control over what is shown and how user input is handled. Canvas supplies methods that allow
your MIDlet to learn about its environment—the size of the display, for example, and which
kinds of events are supported by the device. User interfaces built on Canvas discover the attributes
of a device and attempt to create something that looks reasonable
Using Lists
There are two main types of List, denoted by constants in the Choice interface:
• EXCLUSIVE specifies a list where only one element may be selected. It is akin to a group of radio
buttons.
But with IMPLICIT lists, there’s no opportunity for the user to change his or her mind before confirming
the selection
List provides methods for adding items, removing elements, and examining elements. Each
element in the List has an index.
Managing Items
Items may be added and removed, even while the Form is showing. The order of items is
important
as well; most MIDP implementations will display a form’s items top to bottom and possibly left
to right, scrolling the form vertically as needed if there are more items than available screen
space.
To add an Item to the bottom of a form, use one of the append() methods. The first one can
be used to add any Item implementation. The second two append() methods are strictly for
convenience; behind the scenes, a StringItem or an ImageItem will be created for you.
public int append(Item item)
public int append(String str)
public int append(Image image)
Every item in a form has an index. You can place an item at a specific index (replacing the
previous item at that index) using the method:
public void set(int index, Item item)
Alternately, if you’d like to add an item somewhere in the middle of the form, just supply
the desired index for the new item to the insert() method. Subsequent items will move up by
one index.
public void insert(int index, Item item)
To remove an item from a form, use delete().
public void delete(int index)
To remove all items with one call, use deleteAll().
public void deleteAll()
If you forget what you put in a form, you can find out the number of items and retrieve
them with the following methods:
public int size()
public Item get(int index)
The semantics for managing item commands are nearly identical to the semantics for
managing form commands. You can manage the commands on an Item using addCommand()
and removeCommand(). Note that the command type should be ITEM for commands added to
Item, although no exception will be thrown if this is not true. A command listener may be
assigned using the setItemCommandListener() method. The ItemCommandListener interface
contains a single method, similar to CommandListener’s single method:
public void commandAction(Command c, Item item)
Finally, the Item class includes a layout directive, accessed using getLayout() and setLayout().
Represented by an integer, the layout value is usually a combination of LAYOUT_2 with a
horizontal
value and a vertical value. LAYOUT_2 is a flag to the implementation that the item should be
laid
out using MIDP 2 rules. The previous MIDP version (1.0) had very limited layout control
capabilities. The horizontal values are
• LAYOUT_LEFT
• LAYOUT_RIGHT
• LAYOUT_CENTER
The vertical values are
• LAYOUT_TOP
• LAYOUT_BOTTOM
• LAYOUT_VCENTER
In addition, a layout value may include shrinking or expanding. Shrinking means that an
item’s minimum width or height is used, while expanding means that an item’s size is stretched
to fill the available width or row height. The constants for shrinking and expanding are
• LAYOUT_SHRINK (for width)
• LAYOUT_EXPAND (for width)
• LAYOUT_VSHRINK (for height)
• LAYOUT_VEXPAND (for height)
Finally, an Item’s layout may include a request for a new line before or after the item using
the LAYOUT_NEWLINE_BEFORE or LAYOUT_NEWLINE_AFTER constants. Items are
laid out in Forms much
like text flows on a page, so these constants allow you to request a new row before or after an
item.
Figure 6-5 shows a simple example, three components with the following layouts:
• LAYOUT_2 | LAYOUT_LEFT | LAYOUT_NEWLINE_AFTER
• LAYOUT_2 | LAYOUT_CENTER | LAYOUT_NEWLINE_AFTER
• LAYOUT_2 | LAYOUT_RIGHT | LAYOUT_NEWLINE_AFTER
To create a TextField, you need to supply the label, text value, maximum length, and input
constraints.
public TextField(String label, String text, int maxSize, int constraints)
For an initially empty TextField, pass null for the text parameter.
As with TextBox, the TextField class includes a setInitialInputMode()
DATEFIELD
To create a DateField, specify a label and a type. Three constants in the DateField class
describe the different types:
• DATE displays an editable date.
• TIME displays an editable time.
• DATE_TIME displays both a date and a time.
DateField provides two constructors. The first uses the default time zone, while the second
allows you to specify a TimeZone explicitly:
public DateField(String label, int mode)
public DateField(String label, int mode, TimeZone timeZone)
DateField takes the role of translating between a Date and strings that
humans can read, much like the Calendar class. You can set or get the Date represented by the
DateField using the following methods:
public Date getDate()
public void setDate(Date date)
PERSISTENT STORAGE
In MIDP, persistent storage is centered around record stores. A record store is a small database
that contains pieces of data called records. Record stores are represented by instances of
javax.microedition.rms.RecordStore. The scope of a record store can either be limited to a
single MIDlet suite or be shared between MIDlet suites. Said another way, you can either
restrict a MIDlet to only access record stores that were created by a MIDlet in the same suite,
or you can explicitly allow MIDlets from multiple suites to share a record store .
The RecordStore class serves two purposes. First, it defines an API for manipulating individual
records. Second, it defines an API (mostly static methods) for managing record stores.
In addition, you can decide if you want a shared record store to be writable or read-only.
You can create a shared record store using an alternate openRecordStore() method in the
RecordStore class:
public static RecordStore openRecordStore(String recordStoreName,boolean
createIfNecessary, byte authMode, boolean writable) throws RecordStoreException,
RecordStoreFullException,
RecordStoreNotFoundException
To access a shared record store, you need to know its name, the name of the MIDlet
suite that created it, and the name of the MIDlet suite’s vendor. These names must be the
MIDlet-Name and MIDlet-Vendor attributes in the MIDlet suite JAR manifest or the application
descriptor.
To find out the number of bytes used by a record store, call the following method on a
RecordStore instance:
public int getSize()
You can find out how much more space is available by calling the following method:
public int getSizeAvailable()
Note that this method returns the total space available in the record store, which is not the
same as the amount of record data that is available. That is, there is some overhead associated
with each record in the record store; the getSizeAvailable() method returns the amount of
space available for both record data and overhead.
Adding Records
To add a new record, supply the byte array to the addRecord() method:
public int addRecord(byte[] data, int offset, int numBytes) throws
RecordStoreNotOpenException,RecordStoreException,RecordStoreFullException
The added record will be numBytes long, taken from the data array starting at offset. The
new record’s ID is returned. Most other record operations need this ID to identify a particular
record.
The following code fragment illustrates adding a new record to a record store named rs.
It creates a byte array from a String, and then writes the entire byte array into a new record.
String record = "This is a record";
byte[] data = record.getBytes();
int id = rs.addRecord(data, 0, data.length);
Retrieving Records
You can retrieve a record by supplying the record ID to the following method:
public byte[] getRecord(int recordId)
throws RecordStoreNotOpenException,
InvalidRecordIDException,
RecordStoreException
This method returns a freshly created byte array containing the record with the requested
ID. An alternate version of this method puts the record data into an array that you supply: