Date post: | 21-Jun-2015 |
Category: |
Business |
Upload: | corneliuskoo |
View: | 1,476 times |
Download: | 3 times |
High Level UI Components
Event Handling
Cornelius Koo - 2005
What is Event Handling
• Process of recognizing when an event
occurs and taking an action based on that
event.
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.
Scenario
Listener
• Before MIDlet can accept and process an
event, it must implements Listener
interfaces.
• There are 2 Listeners in MIDP :
1. CommandListener
2. ItemStateListener
public class TestCommandListener
extends MIDlet implements
CommandListener {
...
public void commandAction(Command c,
Displayable s) {…}
}
public class TestItemStateListener
extends MIDlet implements
ItemStateListener {
...
public void itemStateChanged(Item
item) {}
}
Command Object
• Command object is an object that holds
information about an event.
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.
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);
}
…
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.
StringItem & ImageItem
• StringItem and ImageItem are also
subclasses of Item however once
allocated, these objects are static and thus
do not receive/acknowledge events.
Command
1. Label
2. Type
3. Priority
label
• Specifies the text you would associate with
the text.
type
• Specify the type of specific soft button.
priority
• Used by application manager when
arranging items that appear in a menu or
for ordering soft buttons on the display.
Command Types
Command & CommandListener API
Item
• Item is any component that can be added
to the form.
• ChoiceGroup, CustomItem, DateField,
Gauge, ImageItem, Spacer, StringItem,
TextField.
ItemListener
• When an item’s value changed, it will
generate an item event and sent it to
ItemListener. (Except for StringItem and
ImageItem)
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.
Item & ItemListener API
Item
ItemListener
List Select Command
• We can change the select command for
List and set it to our own command.
private List lsHero;
private Command cmOpen = new
Command(“Open",Command.ITEM,1);
private String[] sound = {
"Info",
"Confirmation",
"Warning",
"Alarm",
"Error“
};
...
lsHero = new List("Message Type",
List.IMPLICIT, sound, null);
lsHero.setSelectCommand(cmOpen);
lsHero.setCommandListener(this);
...
public void commandAction(Command arg0,
Displayable arg1) {
if(arg0 == cmOpen){
System.out.println(“Open selected");
System.out.println(
((List)arg1)
.getString(((List)arg1)
.getSelectedIndex())
);
}
}
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.