2.3) FORM : Item Object
The Item abstract class acts as the base class for all components that can be placed on a form. All Item objects have a label:
1 2 | public String getLabel( ); public void setLabel(String s); |
2.3.1) ChoiceGroup
A ChoiceGroup object represents a list of selectable choices to be placed on a Form object. It implements the Choice interface. In this object single choice can be made, or it may allow multiple choices. It has two constructors:
1 2 | public ChoiceGroup(String label, int choiceType); public ChoiceGroup(String label, int choiceType, String[] stringElements, Image[] imageElements); |
The first constructor is used to create an empty choice group, specifying its label and type. Here two choices are available: EXCLUSIVE and MULTIPLE. The second constructor can be used to create a new choice group, specifying its title and type.We can insert, append, or replace choices in this.Each choice has an integer index that represents its position in the list. The first choice starts at 0 and extends to the current size of the list minus one. It has the following methods.
1 2 3 | public int append(String stringElement, Image imageElement); public void insert(int index, String stringElement, Image imageElement); public void set(int index, String stringElement, Image imageElement); |
Here are some examples.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | int save = list.append("save", null);
public void delete(int index);//delete any index in the choice group
public String getString(int index);//retrieve the string element
public Image getImage(int index);//retrieve the image element
public int getSelectedIndex( );
public boolean isSelected(int index);
public setSelectedIndex(int index, boolean selected);//set the currently selected index
public int getSelectedFlags(boolean[] selectedArray); //set the selection state of the entire choice group
public void setSelectedFlags(boolean[] selectedArray);
ChoiceGroup choices = new ChoiceGroup("Method of payment",Choice.EXCLUSIVE);
choices.append("aaa", null); //adds several new choices(Do the same for all)
choices.delete(15);//delete the 15th choice. Similarly insert and append |
Choice can be displayed using:
1 2 3 4 | setCurrent( ).
Form form = new Form("Select");
form.append(choices);
Display.setCurrent(form); |
2.3.2) DateField
A DateField object is a component for representing date and time information. It can be configured to accept date or time information. It has the following constructors:
1 2 | public DateField(String label, int mode); public DateField(String label, int mode, TimeZone timeZone); |
The firstone is used to create a DateField object with the specified label and mode. It has some static fields: DateField.DATE, DateField.TIME, or DateField.DATE_TIME. The DateField.DATE input mode used to set date information, DateField.TIME for clock time information, and DateField.DATE_TIME for both. This one has the following methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public Date getDate( )
public int getInputMode( )
public void setDate(Date date);
public void setInputMode(int mode);
public String toString( ); // output a string-based copy of the date or time
DateField date = new DateField("date", DateField.DATE); //creates a DateField object
/* display a date field */
Form form = new Form("Date Info");
form.append(date);
Display.setCurrent(form);
/* Initialize the date and time before displaying the component */
d = new DateField("Today: ", DateField.DATE);
d.setDate(new Date( ));
form = new Form("Date & Time");
form.append(d);
display.setCurrent(form);
/* Creating a DateField object where the time zone is GMT */
DateField date = new DateField("date", DateField.DATE,TimeZone.getTimeZone("GMT"));
/* Using the default time zone */
DateField date1 = new DateField("date", DateField.DATE);
DateField date2 = new DateField("date", DateField.DATE, TimeZone.getDefault( )); |
2.3.3) Gauge
A Gauge object represents a bar graph display that can be used within a form. It has the following constructor:
1 | public Gauge(String label, boolean interactive, int maxValue, int initialValue); |
This is used to create a new Gauge object with the given label, in interactive or non-interactive mode, with the given maximum and initial values. In interactive mode, we can modify the gauge’s current value; in non-interactive mode, we cannot change the value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public boolean isInteractive( );//Whether the gauge is currently in interactive mode or not
public int getMaxValue( ); //access the Max value
public int getValue( ); //access the current value
public void setMaxValue(int maxValue); //set the Max value
public void setValue(int value);
/* interactive gauge where the maximum value is 20 and the initial value is 0 */
Gauge gauge = new Gauge("graph", true, 20, 0);
/* placing guage on a Form component */
Form form = new Form("item");
form.append(gauge);
/* non-interactive gauge that reflects the progress bar */
Display display = Display.getDisplay(this);
Gauge progressbar = new Gauge("Progress", false, 20, 9);
Form form = new Form("Configuring App);
form.append(progressbar); |
2.3.4)Image and ImageItem
An ImageItem object is an image component that contains a reference to an Image object. Images can either be immutable or mutable. Immutable images are generally created by loading image data.They cannot be modified after creation. Mutable images, are created in off-screen memory and can be modified. A mutable image can be created using static createImage( ) methods of the Image class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | public static Image createImage(int width, int height);
/* Three static createImage( ) methods used to create immutable images */
public static Image createImage(Image image);
public static Image createImage(String name);
public static Image createImage(byte[] imageData, int imageOffset,int imageLength);
/* Creating an immutable image */
Image image = Image.createImage("image1.png");
/* Placing an Image in a Form object */
Form form = new Form("Form");
form.append(image);
/* Measuring the height, width, and mutable status of a image */
public int getHeight( );
public int getWidth( );
public boolean isMutable( );
/* Getting the Graphics object of an image */
public Graphics getGraphics( );
/* Creating an ImageItem object using the ImageItem constructor */
public ImageItem(String label, Image img, int layout, String altText);
/* Methods to access the properties of the ImageItem constructor */
public String getAltText( );
public Image getImage( );
public int getLayout( );
public void setAltText(String altText);
public void setImage(Image img);
public void setLayout(int layout);
/* Creating the ImageItem object using ImageItem constructor */
Image img = Image.createImage("/Example.png");
ImageItem imageItem = new ImageItem("Image", img, ImageItem.LAYOUT_CENTER, "img");
Form form = new Form("Example");
form.append(imageItem); |
2.3.5) StringItem
A StringItem object is a text component item that contain a string, label, and contents. We cannot edit the string but we can edit the label and contents.Here is the constructor..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public StringItem(String label, String contents);
/* Creating a StringItem object */
StringItem si = new StringItem("string", "contents");
public void setText(String s); // Set the StringItem contents
public void setLabel(String l);// Set the label of the StringItem
public String getText( );// Get the StringItem contents
public String getLabel( );//Get the label of the StringItem
/* Creating a StringItem object placing it within a Form object */
Display display = display.getDisplay(this);
StringItem si = new StringItem("String item:\n", "Hello World!");
Form form = new Form("Greetings");
form.append(si);
display.setCurrent(form); |
2.3.6) TextField
TextField object is an editable text component.A TextField has a capacity, which is the number of characters that can be stored in the object. There must be a boundary on the maximum size,which we can get using getMaxSize( ). TextField is used when the MIDlet requires input from the user. A TextField has the constructor:
1 | public TextField(String label, String text, int maxSize, int constraints); |
The constraints field is used to limit the user input like TextField.ANY, TextField.EMAILADDR, TextField.NUMBER, TextField.PASSWD, TextField.PHONENUMBER, and TextField.URL. Here we have some examples..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public int getConstraints( ); //Retrieve the current constraints
public void setConstrants(int c);//Set the current constraints
public int getMaxSize( ); //Gets Maxsize
public void setMaxSize(int size);// Sets Maxsize
public String getString( ); //Retrieve the text in the TextField
public void setString(String s);//Set the text in the TextField
public int size( );//To get the number of characters in the TextField
/* delete, insert, and replace */
public void delete(int offset, int length);
public void insert(char[] data, int offset, int length, int position);
public void insert(String src, int position);
public void setChars(char[] data, int offset, int length);
public int getCaretPosition( ); //To Find the position of the caret or insertion beam
/* Typical sign in form */
Display display = Display.getDisplay(this);
TextField userName = new TextField("LoginID:", "", 10, TextField.ANY);
TextField password = new TextField("Password:", "", 10, TextField.PASSWORD);
Form form = new Form("Sign in");
form.append(userName);
form.append(password);
display.setCurrent(form); |
3) Conclusion
Herewith we discussed about the Graphical user interface concepts in J2ME. Try the example programs with the help of the methods discussed above.These method names are very much standard and easy to remember, so one time practice is enough to become efficient in J2ME GUI Programming






October 20, 2007
J2ME