Forms
The canvas is good when you need to display graphics or animations on the device screen. But sometimes, the MIDlet must ask the user to enter some data (such as user's name, select his favorite animal etc.). This can be hardly achieved with drawing on the canvas and reading from the keypad. The MIDlet can use the following user-interface elements: form, textbox, alert and menu.
The form, as opposed to canvas, may contain user-interface elements such as a text field, a choice group or a gauge. To switch from the canvas (which is the default display) to the form, use 'showForm' procedure. To switch back to canvas, use 'showCanvas' procedure. An alert (showAlert) displays a message on the device display. TextBox (showTextBox) displays large textbox which occupies entire device display. Menu (showMenu) which also occupies whole device display allows user to select an entry. Commands are used for implementing the push-button functionality. Commands can be added to canvas, form, textbox, alert or menu.
| Canvas | Form with form elements | |
![]()
| ![]()
| |
| Menu (with command) | Textbox (with command) | |
![]()
| ![]()
| |
| Alert (with command) | ||
![]() |
Take a look at the following example:
{ the program asks the user for his name and then
displays it in red color }
var okCommand:command;
nameField:integer;
userName:string;
begin
{ create the form on the screen }
okCommand := createCommand('OK', CM_OK, 1);
showForm;
addCommand(okCommand);
nameField := formAddTextField('Enter your name', '', 20, TF_ANY);
{ wait until the user presses the OK button }
while (getClickedCommand <> okCommand) do
delay(100);
userName := formGetText(nameField);
{ draw the user's name on the display in red color }
showCanvas;
setColor(255, 0, 0);
drawText(userName, 5, 5);
repaint;
delay(2000);
end.