In J2ME, is there any method or property which similar to the functionality of gotFocus & lostFocus on control

StackOverflow https://stackoverflow.com/questions/14175412

  •  13-01-2022
  •  | 
  •  

Domanda

In my J2ME app, I want to add a play command when got focus on choicegroup control named as Tones and after lost focus on choicegroup control the command should be removed.

How should I do this?

Update:

Here is my code:

 import javax.microedition.midlet.*;

 import javax.microedition.lcdui.*;

 public class Focusoncontrol extends MIDlet implements CommandListener
 {  
    Display disp;

    TextField Text1, Text2, Text3;
    ChoiceGroup Tones;
    Form frm;
    Command Save, Back, Play;

    public Focusoncontrol()
    {
        disp = Display.getDisplay(this);
        frm  = new Form("Focus demo");
        Text1 = new TextField("Text1", "", 20, 0);
        Text2 = new TextField("Text2", "", 20, 0);
        Text3 = new TextField("Text3", "", 20, 0);
        Tones = new ChoiceGroup("Tones", Choice.POPUP, new String[]{"Tone 1", "Tone 2"}, null);
        Save = new Command("Save", Command.SCREEN, 1);
        Back = new Command("Back", Command.EXIT, 3);
        Play = new Command("Play", Command.OK, 2);

        frm.append(Text1);
        frm.append(Text2);
        frm.append(Tones);
        frm.append(Text3);
        frm.addCommand(Save);
        frm.addCommand(Back);
        frm.setCommandListener(this);

        disp.setCurrent(frm);
    }

    public void startApp() 
    {
    }

    public void pauseApp() 
    {
    }

    public void destroyApp(boolean unconditional) 
    {
    }

    public void commandAction(Command c, Displayable d) 
    {
        if(c == Back)
        {
            destroyApp(true);
            notifyDestroyed();
        }
    }
}

I am not added Play command during initialization of app because I have to add play command on form when Tones(ChoiceGroup control) got focus and removed the command when ChoiceGroup control lost the focus.

È stato utile?

Soluzione

ChoiceGroup is an Item object, and to use commands like you describe you need ItemCommandListener:

A listener type for receiving notification of commands that have been invoked on Item objects. An Item can have Commands associated with it. When such a command is invoked, the application is notified by having the commandAction() method called on the ItemCommandListener that had been set on the Item with a call to setItemCommandListener()...

To set "play" command for the choicegroup, use method Item.addCommand(Command):

Tones.addCommand(Play); // add command to item
Tones.setItemCommandListener(/*... define  item cmd listener*/); // set listener

Above code could be written eg right before invoking disp.setCurrent(frm) in your code snippet.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top