How can you find out when the value in the TextField has been changed?
You must use the ItemStateListener interface and register the listener to the form. The easiest way is to implement ItemStateListener on the form that contains your TextField and put your code into the itemStateChanged method.
class MyClass extends Form implements ItemStateListener
{
public MyClass()
{
setItemStateListener(this);
}
public void itemStateChanged(Item item)
{
...
}
}
Another approach would be to create an anonymous class and register it with the form:
public class MyClass extends MIDlet
{
public void startApp()
{
Form form = new Form("My Test");
// an anonymous class
ItemStateListener listener = new ItemStateListener()
{
public void itemStateChanged(Item item)
{
// do something
}
};
// register for events
form.setItemStateListener(listener);
...
display.setCurrent(form);
}
}
This is just one of the concepts of J2ME I have been learning for the last few days. Will keep posting more...
2 comments:
Hi,
With the itemStateListener for Textfield, does it execute onChange when a new character is typed or is it only when exiting the Textfield?
Thanks for the great post!
Looks like it does execute when you add a character. I think I read there are different implementations, but it looks good.
Post a Comment