×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    16

    Question J2ME Commands and CommandListener

    Hey Everybody

    I have a small and simple problem - Im new to J2ME devel.
    My problem is that I can not get the commands, assigned to soft bottons, to work.

    I know its long a first, but the problem resides in the commandlistener-function: commandAction(c,d).
    The commands for the mainMenu (in and out of the menu) works fine. Just as well when I enter one of the submenus - IO-
    but when I execute one of the options in the submenus and then wish to return to a submenu after execution there is no response.....?
    The Compiler dosnt give me any errors and no execptions are present.

    The code looks like this :

    /*
    * Menu_Test2.java
    *
    * Created on 21. februar 2005, 14:50
    */
    // import all
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;

    /**
    *
    * @author Christian V Petersen
    * @version
    */

    public class Menu_Test2 extends MIDlet implements CommandListener
    {
    private boolean firstTime; // used to display the menus for the first time, later when back is used it is not the first time

    //For the main menu no go/enter command is needed because the a list is used

    private Command BACK = new Command("Back",Command.BACK,0);

    private Command EXIT = new Command("Exit",Command.EXIT,1);

    private Command DONE = new Command("Done",Command.OK,1);


    private Form sub2form;
    private TextBox input;
    private static String Intial_Text = "Enter user name here - max 100";
    public String name;
    private TextField show_user;

    // String arrays for the options in the menus
    String main[] =
    {
    "Submenu1",
    "Submenu2"
    };

    String sub1[] =
    {
    "Enter user name"
    };

    String sub2[] =
    {
    "Show user name"
    };

    private Display display;

    //Menu tree
    private List mainMenu;
    private List subMenu1; // to enter user name
    private List subMenu2; // to display user name

    public Menu_Test2()
    {
    display = Display.getDisplay(this);
    //Instantiate the "menus" and adds commands and comm-List
    // No need foo to initilale the mainMenu, this is done in startApp()

    subMenu1= new List("Submenu1",Choice.IMPLICIT,sub1,null);
    subMenu1.addCommand(BACK);
    subMenu1.addCommand(EXIT);
    subMenu1.setCommandListener(this);

    input = new TextBox("Enter a user name",Intial_Text,100,0);
    input.addCommand(BACK);
    input.addCommand(DONE);
    input.setCommandListener(this);

    subMenu2= new List("Submenu2",Choice.IMPLICIT,sub2,null);
    subMenu2.addCommand(BACK);
    subMenu2.addCommand(EXIT);
    subMenu2.setCommandListener(this);
    sub2form = new Form("User name");
    sub2form.addCommand(DONE);
    sub2form.setCommandListener(this);

    firstTime = true;
    }

    //All the state-functions
    public void destroyApp(boolean destroy)
    {
    input = null;
    Intial_Text = null;
    // No statements here because quit() is def and it calls notifyDestroyed() to the destroyed state
    }

    void quit()
    {
    destroyApp(true);
    notifyDestroyed();
    }

    public void pauseApp()
    {}

    public void startApp()
    {
    if(firstTime==true) // initial run
    {
    mainMenu= new List("Main Menu",Choice.IMPLICIT,main, null);
    mainMenu.addCommand(EXIT);
    mainMenu.setCommandListener(this);
    display.setCurrent(mainMenu);

    firstTime = false;

    }
    }

    public void commandAction(Command c, Displayable d)
    {
    /*
    The d is used to determine in wish menu we are
    and c is used to determine wish commmand has been executed
    */
    if(d.equals(mainMenu)) //we are in the main menu
    {
    // Options from this menu
    if(c==List.SELECT_COMMAND) // otherwise its or exit
    {
    if(d.equals(mainMenu))
    {
    switch(((List)d).getSelectedIndex())
    {
    case 0:
    display.setCurrent(subMenu1);
    break;

    case 1:
    display.setCurrent(subMenu2);
    break;

    }
    // These are the only possible routes from the mainmenu - that is, just display
    }

    }
    else
    {
    quit();
    }

    }// end mainMenu
    else if(d.equals(subMenu1)) // enter a user name
    {
    if(c==BACK)
    {
    display.setCurrent(mainMenu);

    }
    /*else if(c==EXIT)
    {
    quit();
    }*/
    else if(c==List.SELECT_COMMAND)
    {
    display.setCurrent(input);

    /*if(c==DONE)
    {
    // name = input.getString();
    display.setCurrent(mainMenu);
    }*/

    if(c==BACK)
    {
    display.setCurrent(subMenu1);
    }
    /*else if(c==EXIT)
    {
    quit();

    }*/
    }

    }//end subMenu1

    else if(d.equals(subMenu2)) // show the user name
    {
    if(c==BACK)
    {
    display.setCurrent(mainMenu);

    }
    /*else if(c==EXIT)
    {
    quit();
    }*/

    else if(c==List.SELECT_COMMAND)
    {

    show_user = new TextField("User name",name,256,0);
    sub2form.append(show_user);
    display.setCurrent(sub2form);
    }

    }// end subMenu2

    else if(c==EXIT)
    {
    quit();

    }


    }
    }

  2. #2
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    Your commandAction() method doesn't handle Commands from the TextBox called input or the Form called sub2form.

    shmoove

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    16

    Got It

    Hey and Thanks
    Its working now

    Christian V Petersen

Posting Permissions

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