×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    136

    How to use CommandListener

    Code:
    public class TsecondTextBoxMIDlet extends MIDlet {
    	Display display;
        TextBox txt;
        Ticker tickerInfo;
    	Alert alerttime;
    	Alert alertModal;
    	Command cmdTime;
    	Command cmdModal;
    
    	public TsecondTextBoxMIDlet() {
    		txt=new TextBox("TTTTTtt","test",100,TextField.INITIAL_CAPS_WORD);
    		tickerInfo=new Ticker("Welcome to J2ME world");
    		txt.setTicker(tickerInfo);
    		cmdTime=new Command("Time",Command.SCREEN,0);
    		cmdModal=new Command("Modal",Command.SCREEN,1);
    		txt.addCommand(cmdTime);
    		txt.addCommand(cmdModal);
    		//txt.setCommandListener(new TestCommandHandler());
    		// TODO Auto-generated constructor stub
    	}
    
    	protected void destroyApp(boolean unconditional)
    			throws MIDletStateChangeException {
    		// TODO Auto-generated method stub
    
    	}
    
    	protected void pauseApp() {
    		// TODO Auto-generated method stub
    
    	}
    
    	protected void startApp() throws MIDletStateChangeException {
    		// TODO Auto-generated method stub
    		display=Display.getDisplay(this);
    		display.setCurrent(txt);
    
    	}
    
    }
    Code:
    //////////////
    public class TestCommandHandler implements CommandListener {
    
    	public void commandAction(Command c, Displayable d) {
    		// TODO Auto-generated method stub
    		TextBox txt=(TextBox)d;
    		if(c==txt.cmdTime)
    			
    		{
    			System.out.println("*****");
    		}
    
    	}
    
    }
    The system does not know cmdTime in the TestCommandHandler.
    How it should be done?

  2. #2
    Registered User
    Join Date
    Dec 2009
    Location
    Bangalore/Mysore, Karnataka, India.
    Posts
    43

    Re: How to use CommandListener

    Hi,
    Code:
    if(c==txt.cmdTime)
    	{
    		System.out.println("*****");
    	}
    I don't understand what made you use it. Did you not get any error while compiling? Could you get to run this MIDlet??

    BTW, you can try below code. If you have command listener inside the MIDlet class itself, then you can use,
    if(c == cmdTime) directly. Else, you can compare the label of command fired as below.
    Code:
    if(c.getLabel().equalsIgnoreCase("Time"))
    	{
    		System.out.println("*****");
    	}
    -Roopesh.
    (http://trivialdust.blogspot.com/)

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: How to use CommandListener

    Please refer to the FN Wiki Code example - http://wiki.forum.nokia.com/index.ph...nds_in_Java_ME

    In your Midlet class, uncomment the line -- txt.setCommandListener(new TestCommandHandler());
    and make txt as public static variable and use it with the Midlet Name here, then it should work, else it will give compilation error.

    best to merge these classes in one like this:

    Code:
    public class TsecondTextBoxMIDlet extends MIDlet {
    	Display display;
        TextBox txt;
        Ticker tickerInfo;
    	Alert alerttime;
    	Alert alertModal;
    	Command cmdTime;
    	Command cmdModal;
    
    	public TsecondTextBoxMIDlet() {
    		txt=new TextBox("TTTTTtt","test",100,TextField.INITIAL_CAPS_WORD);
    		tickerInfo=new Ticker("Welcome to J2ME world");
    		txt.setTicker(tickerInfo);
    		cmdTime=new Command("Time",Command.SCREEN,0);
    		cmdModal=new Command("Modal",Command.SCREEN,1);
    		txt.addCommand(cmdTime);
    		txt.addCommand(cmdModal);
    		txt.setCommandListener(this);
    		// TODO Auto-generated constructor stub
    	}
    
          public void commandAction(Command c, Displayable d) {
    		// TODO Auto-generated method stub
    		TextBox txt=(TextBox)d;
    		if(c==txt.cmdTime)
    			
    		{
    			System.out.println("*****");
    		}
    
    	}
    
    
    
    	protected void destroyApp(boolean unconditional)
    			throws MIDletStateChangeException {
    		// TODO Auto-generated method stub
    
    	}
    
    	protected void pauseApp() {
    		// TODO Auto-generated method stub
    
    	}
    
    	protected void startApp() throws MIDletStateChangeException {
    		// TODO Auto-generated method stub
    		display=Display.getDisplay(this);
    		display.setCurrent(txt);
    
    	}
    
    }
    Thanks,
    Ekta

  4. #4
    Super Contributor
    Join Date
    Sep 2008
    Location
    Noida, U.P.
    Posts
    1,330

    Re: How to use CommandListener

    Quote Originally Posted by komomo View Post
    Code:
    public class TsecondTextBoxMIDlet extends MIDlet {
    	Display display;
        TextBox txt;
        Ticker tickerInfo;
    	Alert alerttime;
    	Alert alertModal;
    	Command cmdTime;
    	Command cmdModal;
    
    	public TsecondTextBoxMIDlet() {
    		txt=new TextBox("TTTTTtt","test",100,TextField.INITIAL_CAPS_WORD);
    		tickerInfo=new Ticker("Welcome to J2ME world");
    		txt.setTicker(tickerInfo);
    		cmdTime=new Command("Time",Command.SCREEN,0);
    		cmdModal=new Command("Modal",Command.SCREEN,1);
    		txt.addCommand(cmdTime);
    		txt.addCommand(cmdModal);
    		//txt.setCommandListener(new TestCommandHandler());
    		// TODO Auto-generated constructor stub
    	}
    
    	protected void destroyApp(boolean unconditional)
    			throws MIDletStateChangeException {
    		// TODO Auto-generated method stub
    
    	}
    
    
    
    	protected void pauseApp() {
    		// TODO Auto-generated method stub
    
    	}
    
    	protected void startApp() throws MIDletStateChangeException {
    		// TODO Auto-generated method stub
    		display=Display.getDisplay(this);
    		display.setCurrent(txt);
    
    	}
    
    }
    Code:
    //////////////
    public class TestCommandHandler implements CommandListener {
    
    	public void commandAction(Command c, Displayable d) {
    		// TODO Auto-generated method stub
    		TextBox txt=(TextBox)d;
    		if(c==txt.cmdTime)
    			
    		{
    			System.out.println("*****");
    		}
    
    	}
    
    }
    The system does not know cmdTime in the TestCommandHandler.
    How it should be done?

    What do you mean by this txt.cmdTime?? is textbox class have any command cmdTime.. call the cmdTime by its class object which is TsecondTextBox or set it static one and call it by class reference.. Im unable to see in ur code that where u have been set the commandlistener for ur commands as well.. Please follow some guideline before writinng any code.. Check and verify..
    thanks,
    jitu_goldie..

    KEEP TRYING..

Similar Threads

  1. Thread and CommandListener
    By Rambajzek in forum Mobile Java General
    Replies: 4
    Last Post: 2008-08-05, 03:34
  2. CommandListener
    By ssandor in forum Mobile Java General
    Replies: 1
    Last Post: 2004-07-30, 12:52
  3. about commandlistener
    By linbo75 in forum Mobile Java General
    Replies: 2
    Last Post: 2004-02-20, 09:00
  4. CommandListener not working
    By stoun in forum Mobile Java General
    Replies: 0
    Last Post: 2003-04-16, 15:32
  5. CommandListener in more than 2 classes
    By stoun in forum Mobile Java General
    Replies: 1
    Last Post: 2003-04-15, 13:05

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×