0% found this document useful (0 votes)
13 views35 pages

TOPIC 4_SLIDE NOTES

The document provides an overview of List and Form objects in computer science, detailing their functionalities and methods. It explains how to create and manipulate List objects, including methods for adding, removing, and modifying elements, as well as introduces Form objects and their associated Item classes. Additionally, it includes MIDlet examples demonstrating the practical application of these concepts.

Uploaded by

Blueprint Mih
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views35 pages

TOPIC 4_SLIDE NOTES

The document provides an overview of List and Form objects in computer science, detailing their functionalities and methods. It explains how to create and manipulate List objects, including methods for adding, removing, and modifying elements, as well as introduces Form objects and their associated Item classes. Additionally, it includes MIDlet examples demonstrating the practical application of these concepts.

Uploaded by

Blueprint Mih
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Page 1 of 35 Dr.

Mwakondo PhD (Computer Science) UoN

Topic4: List and Forms


-Introduction to List Object
-Method members of List Object
-MIDlet Example1
-Introduction to Form Object
-Item class for Form Object
- Form Objects Layout
- Method Members of Form Object
-MIDlet Example2
-Exercise
Page 2 of 35 Dr. Mwakondo PhD (Computer Science) UoN

4.1 Introduction to List Object


–Object that allows user to select items from a group of
choices.
–Items to be selected are known as elements and each is
represented by a text string and an optional image.
–List object supports selection of single or multiple
elements.
–List object implements choice interface that defines the
types of selections for the elements.
–Three types of list objects as defined by the choice
interface are:
 Exclusive (Choice.EXCLUSIVE)
oCan select one element at a time then confirm
 Multiple (Choice.MULTIPLE)
oCan select many elements at a time then confirm
Page 3 of 35 Dr. Mwakondo PhD (Computer Science) UoN

 Implicit (Choice.IMPLICIT)
oSpecial exclusive list that combines selection and
confirmation as one process
Page 4 of 35 Dr. Mwakondo PhD (Computer Science) UoN

4.2 Member Methods for List Object


–Can control different aspects of a list object by calling
relevant methods or using the right constructor.
a. Constructor method
 Can create the list object in one of the two ways:
1. Create an empty list object and add elements later
Format:
List listVariable = new List(“title”,listType);
e.g List("ReservationType”,Choice.IMPLICIT);
2. Create a list populated with elements i.e. create elements
first then list with these elements
Format:
String elements[] = {“element1”,“element2”,…,“element”};
List listVariable=new List (“title”, listType, elements[], null);
e.g String items[]={“Airplane”,”Hotel”,”car”};
List("Reserve T”,Choice.IMPLICIT,items,null);
Page 5 of 35 Dr. Mwakondo PhD (Computer Science) UoN

b. append method
 Used to add elements in the list object i.e. end of list:
Format:
listVariable.append(“element”, null);
e.g. rervationList.append("Airplane”, null);

c. insert method
 Used to add elements in the list object at a specified
position(index) in the list:
Format:
listVariable.insert(index, “element”, null);
e.g. rervationList.insert(0,"Airplane”, null);
Page 6 of 35 Dr. Mwakondo PhD (Computer Science) UoN

d. set method
 Used to replace an element with a new one at a
specified position in the list:
Format:
AlertVariable.set(index, “newelement”,null);
e.g. rervationList.set(0,”Ferry”,null);

e. delete method
 Used to remove an element at a specified position in
the list:
Format:
AlertVariable.delete(index);
e.g. rervationList.delete(0);
Page 7 of 35 Dr. Mwakondo PhD (Computer Science) UoN

e. deleteAll method
 Used to remove every element in the list:
Format:
AlertVariable.deleteAll();
e.g. rervationList.deleteAll();

f. size method
 Used to return number of elements in the list:
Format:
int number = AlertVariable.size();
e.g. int n = rervationList.size();

g. getString method
 Used to return text label of an element at a specified
position in the list:
Page 8 of 35 Dr. Mwakondo PhD (Computer Science) UoN

Format:
String label =listVariable.getString(index);
e.g. String name=reservationList.getString(2);

h. setFitPolicy method
 Used to tell the list object on how to handle elements
whose text is wider than the screen:
 These policies are defined in choice interface as:
 wrap to multiple lines; Choice.TEXT_WRAP_ON
 truncate at the edge; Choice.TEXT_WRAP_OFF
 apply default; Choice.TEXT_WRAP_DEFAULT
Format:
AlertVariable.setFitPolicy(policyType type);
e.g. myList.setFitpolicy(Choice.TEXT_WRAP_ON);
Page 9 of 35 Dr. Mwakondo PhD (Computer Science) UoN

i. getSelectedIndex method
 Used to return index of a selected element in the list:
Format:
int index =listVariable.getSelectedIndex();
e.g. index=reservationList.getSelectedIndex();

l. isSelected method
 Used to return the selection status of the specified
element in the list:
Format:
Boolean status =listVariable.isSelected(index);
e.g. status = reservationList.isSelected(0);
Page 10 of 35 Dr. Mwakondo PhD (Computer Science) UoN

4.3a MIDlet Example


import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.*;
/**
* @author MWAKONDO
*/
public class mylistDemo1 extends MIDlet {
private Alert myAlert;
private List myreservationList;
private Display display;

public mylistDemo1(){
myreservationList = new List("RESERVATION
TYPE",Choice.EXCLUSIVE);
myreservationList.append("Airplane", null);
myreservationList.append("Hotel", null);
myreservationList.append("Car", null);
myreservationList.set(2,"Ferry", null);
Page 11 of 35 Dr. Mwakondo PhD (Computer Science) UoN

display = Display.getDisplay(this);
}
public void startApp() {
display.setCurrent(myreservationList);
int n = myreservationList.size();
myAlert = new Alert("SIZE OF MY RESERVATION
LIST IS:");
myAlert.setString(Integer.toString(n));
display.setCurrent(myAlert);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
Page 12 of 35 Dr. Mwakondo PhD (Computer Science) UoN

4.3b MIDlet Example


public class myListDemo2 extends MIDlet {
private List myreservationList;
private Display display;
//private String myElements[];

public myListDemo2(){
String myElements[] = {"Airplane","Hotel","Car"};
myreservationList = new List("RESERVATION
TYPE",Choice.EXCLUSIVE,myElements,null);

display = Display.getDisplay(this);
}
public void startApp() {
display.setCurrent(myreservationList);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
Page 13 of 35 Dr. Mwakondo PhD (Computer Science) UoN

4.4 Introduction to Form Object


–Screen object that allows user to include an arbitrary
collection of user-interface controls, called items.
–Each item is represented by an instance of the item class.
–Form object implements item class that represents each
item to be included in the form.
–Forms attempt to lay out items from left to right in rows
and stacking rows top to bottom.
–Item class includes plumbing that allows some control
over the layout of individual items.
–Forms that will be large to fit on the screen will
automatically be made scrollable, i.e. not good for user.
Page 14 of 35 Dr. Mwakondo PhD (Computer Science) UoN

4.5 Toolbox of Item Class


–Item class includes all items that can be added in the
form and used to create forms and provides two methods
for each i.e. getLabel() and setLabel().
1. StringItem
 Represents simple text label
Format:
StringItem itVariable=new StringItem(“label”,“text”);
e.g. StringItem myItem=new StringItem("Name:”,”Ali”);
Member Methods:
a) myItem.getLabel();//returns item’s label
b) myItem.setLabel(“label”);//replace label
c) myItem.getText();//returns item’s text
d) myItem.setText("text”);//replace text
Page 15 of 35 Dr. Mwakondo PhD (Computer Science) UoN

2. Spacer
 Represents empty space in the form i.e. used for layout
purposes
Format:
Spacer spVariable=new Spacer(minWidth, minHeight);
e.g. Spacer mySpacer=new Spacer(12,6);
Member Methods:
a) none

3. TextField
 Represents a labeled editable field where text can be
entered directly by either typing on the keyboard or
clicking number buttons.
Page 16 of 35 Dr. Mwakondo PhD (Computer Science) UoN

 Has restrictions on input content same as in TexBoxes


i.e. six constraint settings for restricting content in the
TextField:
o Any – (TextField.ANY)
o Email address – (TextField.EMAILADDR)
o Numeric – (TextField.NUMERIC)
o Phone number – (TextField.PHONENUMBER)
o Url – (TextField.URL)
o Decimal – (TextField.DECIMAL)
Format:
TextField txtVariable=new TextField(“label”,“text”,maxsize,constraint);
e.g. TextField myField=new TextField("Name:”,””,10,TextField.ANY);

Member Methods:
a) myItem.getLabel();//returns item’s label
b) myItem.setLabel(“label”);//replace label
Page 17 of 35 Dr. Mwakondo PhD (Computer Science) UoN

c) myItem.getText();//returns item’s text


d) myItem.setText("text”);//replace text

4. ImageItem
 Represents an image that can be added in the form
 Has three associated data items:
o label – may be displayed with the image
o layout – determines placement of an image
o alternate text – displayed if an image cannot be
shown
Note:
 ImageItem layout can be controlled using layout
properties of the item class as discussed under the Form
Layout below:
 Possible values for layout parameter:
Page 18 of 35 Dr. Mwakondo PhD (Computer Science) UoN

oLAYOUT_DEFAULT
oCombine horizontal values with vertical values
using bitwise operator:
oHorizontal values include: LAYOUT_LEFT,
LAYOUT_CENTER, or LAYOUT_RIGHT
oVertical values include:
LAYOUT_NEWLINE_BEFORE or
LAYOUT_NEWLINE_AFTER
Format:
ImageItem imgVariable=new ImageItem(label,image,layout,alternatetext);
e.g.
ImageItem myImage=new ImageItem (null,image,
ImageItem.LAYOUT_DEFAULT,”Waiting for the Image”);
OR
ImageItem imgVariable=new ImageItem(label,image,layout,alternatetext,
appearance);
Page 19 of 35 Dr. Mwakondo PhD (Computer Science) UoN

e.g.
ImageItem myImage=new ImageItem (null,image,
ImageItem.LAYOUT_DEFAULT,”Waiting for the Image”, null);

Member Methods:
a) myImage.getLabel();//returns image label
b) myImage.setLabel(“label”);//replace label
c) myImage.getAltText();//return alternate text
d) myItem.setAltText("text”);//replace alt.text
e) myImage.getImage();//returns image
f) myImage.setImage(“Image”);//replace image
g) myImage.getLayout();//return layout
h) myImage.setLayout(int Layout);//new layout
i) myImage.getAppearanceMode();//return mode

5. DateField
Page 20 of 35 Dr. Mwakondo PhD (Computer Science) UoN

 Represents a labeled editable field where dates, times, or


both can be entered directly by either typing on the
keyboard or clicking number buttons.
 There are three types of DateFields:
o Date only field –displays editable date
o Time only field – displays editable time
o Both date and time field – displays both editable
date and time
 Has three associated data items:
o label – specifies the label
o type – specifies the type
 Three constants in the DateField class describe the three
different types:
o DateField.DATE –displays editable date
o DateField.TIME – displays editable time
Page 21 of 35 Dr. Mwakondo PhD (Computer Science) UoN

o DateField.DATE _TIME– displays both date and


time
 DateField class provides two constructors: the first uses
the default TimeZone while the second allows you to
specify TimeZone explicitly.

Format:
DateField dfVariable=new DateField(label,mode);
e.g.
DateField myDate=new DateField(“Date of Birth”,DateField.DATE);
OR
DateField dfVariable=new DateField(label,mode,TimeZone);
e.g.
DateField myDate=new DateField(“Date of Birth”,DateField.DATE,
TimeZone.DEFAULT);
Page 22 of 35 Dr. Mwakondo PhD (Computer Science) UoN

Member Methods:
a) myDate.getLabel();//returns date label
b) myDate.setLabel(“label”);//replace label
c) myDate.getDate();//returns date
d) myDate.setDate(Date);//replace date
e) myImage.getInputMode();//return type
f) myItem.setInputMode(Type);//replace

6. Gauge
 Represents a simple indicator showing progress or
status.
 Has three associated data items:
o label – specifies the label
o type – specifies the type
o maxValue – specifies maximum range value
o initValue – specifies initial value of range
Page 23 of 35 Dr. Mwakondo PhD (Computer Science) UoN

 There are two types of Gauge objects:


o Interactive –user can modify gauge value i.e.
using left and right buttons
o non-interactive – user cannot modify value
 Three types of non-interactive gauge are:
owith maximum value (incremental)
owith no maximum value (continuous)
 Boolean in the constructor is true if gauge is interactive
otherwise false.
Format:
Gauge gageVariable=new Gauge(“label”,boolean,maxValue,initValue);
e.g. Gauge interactiveGauge=new Gauge("Power”,true,24,2);
e.g. GaugeincrementalGauge=new Gauge("Power”,false,
Gauge.INDEFINITE, Gauge.INCREMENTAL_UPDATING);
e.g. GaugecontinuousGauge=new Gauge("Power”,false,
Gauge.INDEFINITE, Gauge.CONTINUOUS_RUNNING);
Page 24 of 35 Dr. Mwakondo PhD (Computer Science) UoN

Member Methods:
a) myGauge.getLabel();//returns date label
b) myGauge.setLabel(“label”);//replace label
c) myGauge.getValue();//returns value
d) myGauge.setValue(Value);//replace value
e) myGauge.getMaxValue();//returns value
f) myGauge.setMaxValue(Value);//replace value
g) myGauge.setLayout(Layout);//sets layout
h) myGauge.isInteractive();//returns type

7. ChoiceGroup
 Represents a list showing choices.
 ChoiceGroup object implements choice interface that
defines the types of selections for the elements.
 Its exactly similar to the normal list.
Page 25 of 35 Dr. Mwakondo PhD (Computer Science) UoN

Note:
–Some of these items may include their own commands in
addition to commands that may be provided by the form
container.
Page 26 of 35 Dr. Mwakondo PhD (Computer Science) UoN

4.6 Form Layout


–Forms attempt to lay out items left-to-right in rows and
stacking rows top-to-bottom.
–Also, Item class includes plumbing that allows some
control over the layout of individual items.
–Items have properties related to layout control
information and can use methods to set or get them
 Item.MINIMUMSIZE -computed by implementation
1. getMinimumWidth(),
2. getMinimumHeight(),
 Item.PREFERREDSIZE -computed by manager /user
1. setPreferredSize(w,h), -default values (-1,-1)
which means manager to figure out best size
2. getPreferredWidth(),
3. getPreferredHeight().
Page 27 of 35 Dr. Mwakondo PhD (Computer Science) UoN

 Item.LAYOUT –
1. setLayout(LAYOUT_2,h,v,s)
 LAYOUT_2 – item laid out using MIDP2 rules
 h – horizontal value/position i.e. possible values:
oLAYOUT_RIGHT
oLAYOUT_LEFT
oLAYOUT_CENTER
 v – vertical value/position i.e. possible values:
oLAYOUT_TOP
oLAYOUT_BOTTOM
oLAYOUT_VCENTER
 s – space before or after item i.e. possible values:
oLAYOUT_NEWLINE_BEFORE
oLAYOUT_NEWLINE_AFTER
2. getLayout()
Page 28 of 35 Dr. Mwakondo PhD (Computer Science) UoN

4.7 Member Methods for Form Object


–Can control different aspects of a Form object by calling
relevant methods or using the right constructor.
a. Constructor method
 Can create the form object in the following way:
1. Create an empty form object and add items later
Format:
Form frmVariable = new Form(“title”);
e.g Form("My Form”);
b. append method
 Used to add items in the form object i.e. in a flow
layout way:
Format:
frmVariable.append(AnyItem);
e.g. myForm.append(stringItem);
Page 29 of 35 Dr. Mwakondo PhD (Computer Science) UoN

Note:
 Every item in the form has an index you can use to
retrieve them.
c. size method
 Used to find out the number of items in the form
object.
Format:
Int n = frmVariable.size();
e.g. n = myForm.size();
d. get method
 Used to retrieve an item in the form object.
Format:
Item thisItem= frmVariable.get(index);
e.g. Item thisItem = myForm.get(0);
Page 30 of 35 Dr. Mwakondo PhD (Computer Science) UoN

e. insert method
 Used to add items in the form object at a specified
position (index):
Format:
frmVariable.insert(index, “itVariable”);
e.g. StringItem myItem=new StringItem("Name:”,”Ali”);
myForm.insert(0, myItem);
f. set method
 Used to replace an item with a new one at a specified
position in the form:
Format:
StringItem itVariable=new StringItem(“label”,“text”);
frmVariable.set(index, “itVariable”);
e.g. StringItem myItem=new StringItem("Name:”,”Ali”);
myForm.set(0,myitem);
Page 31 of 35 Dr. Mwakondo PhD (Computer Science) UoN

g. delete method
 Used to remove an item at a specified position in the
form:
Format:
frmVariable.delete(index);
e.g. myForm.delete(0);
h. deleteAll method
 Used to remove every item in the form:
Format:
frmVariable.deleteAll();
e.g. myForm.deleteAll();
Page 32 of 35 Dr. Mwakondo PhD (Computer Science) UoN

4.8a MIDlet Example


import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* @author MWAKONDO
*/
public class FormExample extends MIDlet {

private Display mDisplay;


private Form myForm;
private StringItem myString1;
private StringItem myString2;
private StringItem myString3;
private StringItem myString4;
private DateField myDateField;
private TextField myTxtField;
private ChoiceGroup myGender;
private Gauge mInteractiveGauge;
private Gauge mIncrementalGauge;
private Gauge mContinuousGauge;
Page 33 of 35 Dr. Mwakondo PhD (Computer Science) UoN

public FormExample() {
myForm = new Form("Form Items");

myString1 = new StringItem("SECTION 1:", "This Form


Section Will Display DateField Item");
myForm.append(myString1);

myDateField = new DateField("ToDays Date",DateField.DATE);


//myDateField.setDate();
myForm.append(myDateField);

myString2 = new StringItem("SECTION 2:", "This Form


Section Will Display TextField Item");
myForm.append(myString2);

myTxtField = new TextField("Your


Name:","",10,TextField.ANY);
myForm.append(myTxtField);

myString3 = new StringItem("SECTION 3:", "This Form


Section Will Display ChoiceGroup Item");
myForm.append(myString3);
Page 34 of 35 Dr. Mwakondo PhD (Computer Science) UoN

String gender[] = {"Male", "Female"};


myGender = new ChoiceGroup("Your
Gender:",ChoiceGroup.EXCLUSIVE,gender,null);
myForm.append(myGender);

myString4 = new StringItem("SECTION 4:", "This Form


Section Will Display Gauge Item");
myForm.append(myString4);

mInteractiveGauge = new Gauge("Interactive", true, 5, 2);


mInteractiveGauge.setLayout(Item.LAYOUT_2);
myForm.append(mInteractiveGauge);

mContinuousGauge = new Gauge("Non-I continuous", false,


Gauge.INDEFINITE, Gauge.CONTINUOUS_RUNNING);
mContinuousGauge.setLayout(Item.LAYOUT_2);
myForm.append(mContinuousGauge);

mIncrementalGauge = new Gauge("Non-I incremental", false,


Gauge.INDEFINITE, Gauge.INCREMENTAL_UPDATING);
mIncrementalGauge.setLayout(Item.LAYOUT_2);
Page 35 of 35 Dr. Mwakondo PhD (Computer Science) UoN

myForm.append(mIncrementalGauge);
}
public void startApp() {
if (mDisplay == null) mDisplay = Display.getDisplay(this);
mDisplay.setCurrent(myForm);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
}

You might also like