Search code examples
java-melabelmidplcdui

Is it possible to dynamically set commands for a j2me element?


I have a StringItem which works like a button. The standard menu items(Back on the left side and Next on the right side are always present). The thing I want to do is to leave Back menu item on the left and change Next menu item's label. Is it possible?

I tried to create a new Command with the necessary label and add to the stringiteim but it doesn't replace Next command, it just creates a menu item called Menu with subitems- Next and My command label.

How can I remove Next or change its label?


Solution

  • Command API does not allow to set new label, so your only option as you correctly guessed was to add the new command. You just forgot one thing...

    ...I tried to create a new Command with the necessary label and add to the stringitem but it doesn't replace Next command, it just creates a menu item called Menu with subitems- Next and My command label...

    ...when doing above, you forgot to remove "Next" command. Code to replace command should be about as follows:

        myForm.removeCommand(nextCommand); // removes "Next" cmd
        myForm.addCommand(myCommand); // adds "My" cmd
    

    or, if you use commands associated with Item (ItemCommandListener API) instead of Form,

        // myStringItem below is your StringItem above
        myStringItem.removeCommand(nextCommand); // removes "Next" cmd
        myStringItem.addCommand(myCommand); // adds "My" cmd