12 high level ui event handling
TRANSCRIPT
![Page 1: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/1.jpg)
High Level UI Components
Event Handling
Cornelius Koo - 2005
![Page 2: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/2.jpg)
What is Event Handling
• Process of recognizing when an event
occurs and taking an action based on that
event.
![Page 3: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/3.jpg)
3 Steps in Event Management
1.The hardware must recognize that something
has occured : button pressed, mouse clicked,
mouse hover, an adapter plugged in etc.
2.The software on the device needs to be notified
of the event.
3. A message from the application manager will be
sent to the MIdlet. The message would contain
information about the event, so that we can
process it.
![Page 4: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/4.jpg)
Scenario
![Page 5: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/5.jpg)
Listener
• Before MIDlet can accept and process an
event, it must implements Listener
interfaces.
• There are 2 Listeners in MIDP :
1. CommandListener
2. ItemStateListener
![Page 6: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/6.jpg)
public class TestCommandListener
extends MIDlet implements
CommandListener {
...
public void commandAction(Command c,
Displayable s) {…}
}
![Page 7: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/7.jpg)
public class TestItemStateListener
extends MIDlet implements
ItemStateListener {
...
public void itemStateChanged(Item
item) {}
}
![Page 8: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/8.jpg)
Command Object
• Command object is an object that holds
information about an event.
![Page 9: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/9.jpg)
Processing Events
• Processing events steps :
1. Create a Command object to hold
information about an event.
2. Add the Command to a Form, Textbox,
List or Canvas.
3. Add a "listener" to the above Form,
Textbox, etc.
![Page 10: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/10.jpg)
private Display display;
private Command cmExit;
private Form fmMain;
public TestDisplayable() {
super();
display = Display.getDisplay(this);
fmMain = new Form("Displayable Form");
cmExit = new Command("Exit", Command.EXIT, 1);
fmMain.addCommand(cmExit);
fmMain.setCommandListener(this);
}
…
![Page 11: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/11.jpg)
Item Object
• Any components that can be added to a Form.
• ChoiceGroup, DateField, Gauge and
TextField are all subclasses of Item and
each can process events.
![Page 12: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/12.jpg)
StringItem & ImageItem
• StringItem and ImageItem are also
subclasses of Item however once
allocated, these objects are static and thus
do not receive/acknowledge events.
![Page 13: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/13.jpg)
Command
1. Label
2. Type
3. Priority
![Page 14: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/14.jpg)
label
• Specifies the text you would associate with
the text.
![Page 15: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/15.jpg)
type
• Specify the type of specific soft button.
![Page 16: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/16.jpg)
priority
• Used by application manager when
arranging items that appear in a menu or
for ordering soft buttons on the display.
![Page 17: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/17.jpg)
Command Types
![Page 18: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/18.jpg)
Command & CommandListener API
![Page 19: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/19.jpg)
Item
• Item is any component that can be added
to the form.
• ChoiceGroup, CustomItem, DateField,
Gauge, ImageItem, Spacer, StringItem,
TextField.
![Page 20: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/20.jpg)
ItemListener
• When an item’s value changed, it will
generate an item event and sent it to
ItemListener. (Except for StringItem and
ImageItem)
![Page 21: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/21.jpg)
When it is called ?
• If an Item has changed, itemStateChanged() must be called for the changed Item before it will acknowledge changes in a subsequent Item.
• If a MIDlet makes a change to an Item (not a user interaction), itemStateChanged() will not be called.
• If the device running the MIDlet can recognize when a user has moved from one Item to another, itemStateChanged() must be called when leaving one Item and before getting to the next.
![Page 22: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/22.jpg)
Item & ItemListener API
Item
ItemListener
![Page 23: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/23.jpg)
List Select Command
• We can change the select command for
List and set it to our own command.
![Page 24: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/24.jpg)
private List lsHero;
private Command cmOpen = new
Command(“Open",Command.ITEM,1);
private String[] sound = {
"Info",
"Confirmation",
"Warning",
"Alarm",
"Error“
};
![Page 25: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/25.jpg)
...
lsHero = new List("Message Type",
List.IMPLICIT, sound, null);
lsHero.setSelectCommand(cmOpen);
lsHero.setCommandListener(this);
...
![Page 26: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/26.jpg)
public void commandAction(Command arg0,
Displayable arg1) {
if(arg0 == cmOpen){
System.out.println(“Open selected");
System.out.println(
((List)arg1)
.getString(((List)arg1)
.getSelectedIndex())
);
}
}
![Page 27: 12 High Level UI Event Handling](https://reader034.vdocuments.us/reader034/viewer/2022052621/5586d873d8b42a524a8b46e2/html5/thumbnails/27.jpg)
Reference
• Core J2ME Technology and MIDP. John
W. Muchow. Prentice Hall PTR, 2002.
• Enterprise J2ME: Developing Mobile
Java Applications. Michael Juntao Yuan.
Prentice Hall PTR, 2003.
• J2ME in A Nutshell. Kim Topley. Oreilly,
2002.