1 Replies - 9492 Views - Last Post: 17 August 2010 - 11:08 PM

#1 elf_rox_2007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 25-July 10

J2ME TextField Events or Key Listeners

Posted 17 August 2010 - 07:02 AM

Hello eveybody,

I am new to J2ME Programming so i am stuck with the listeners thing.

When programming in JavaSE, we can use listeners to handle events, however after seeing the J2ME reference i see that there are very few listeners which have confused me.

I am writing a simple J2ME app in which there are 3 textfields. As soon as the user types a char in first textfield the result must be displayed in second and third textfield i.e. I want to handle the events of changes in data of TextField. Can any body tell what listener to use for it and how to do it ???

Here is what i have tried to make the gui, but can't go further as I dont know how to listen to keypad events or TextField changes....

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package kalc;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import kalc.TextFieldItem;

/**
 * @author Administrator
 */
public class KalcMIDlet extends MIDlet implements CommandListener{

    private Command exitCommand;
    private Command aboutCommand;

    private Form mainForm;

    
    private TextField tf2 = new TextField("Hexadecimal Output:","",15,TextField.NUMERIC);
    private TextField tf3 = new TextField("Binary Output:","",15,TextField.NUMERIC);
    private TextField tf4 = new TextField("Octal Output:","",15,TextField.NUMERIC);

    public KalcMIDlet(){

        exitCommand = new Command ("EXIT",Command.EXIT,2);
        aboutCommand = new Command("ABOUT",Command.ITEM,1);

        mainForm = new Form("Calc");
    }

    
    public void startApp() {
        
        mainForm.append(tf);
        mainForm.append(tf2);
        mainForm.append(tf3);
        mainForm.append(tf4);

        mainForm.addCommand(exitCommand);
        mainForm.addCommand(aboutCommand);

        mainForm.setCommandListener(this);

        Display.getDisplay(this).setCurrent(mainForm);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable s) {

        if(c==exitCommand)
        {
            destroyApp(false);
            notifyDestroyed();
        }
        if(c==aboutCommand)
        {
            Alert a = new Alert("About", "About text", null, AlertType.INFO);
            a.setTimeout(Alert.FOREVER);
            Display.getDisplay(this).setCurrent(a);
        }

    }
}




Thanks in advance....

Is This A Good Question/Topic? 0
  • +

Replies To: J2ME TextField Events or Key Listeners

#2 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: J2ME TextField Events or Key Listeners

Posted 17 August 2010 - 11:08 PM

You will need to use an ItemStateListener.
First, your class needs to implement the ItemStateListener interface. Then you need to inform your form that you have defined an itemStateListener in the same class by using mainForm.setItemStateListener(this);

Then insert your code into the itemStateChanged() function like this:
public void itemStateChanged(Item i)
{
   /// insert code here
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1