Results 1 to 6 of 6

Thread: Event Handling

  1. #1
    luisarca is offline Member
    Join Date
    May 2007
    Posts
    11
    Rep Power
    0

    Default Event Handling

    Hi to every one!

    I have problems with my program, because I have 4 different list:The first list show the possibilities to choose among "photos Russia", "photos Finland" and "Photos Spain". The second list is the Photos Russia list, and the user can choose between 2 pictures to watch. The third list is Photos Finland, and the last one is Phostos Spain.
    So, when the user choose, from the first list, "photos Russia",The user must see on the mobile screen the name of the pictures. Then, he can choose one of them, and the picture will be showed on the screen.
    My problem is with the method Command Action, because it doesnt work properly. The function is as follows:
    public void commandAction(Command c, Displayable d){

    if (c == back){ //Selecciono comando “Atrás”

    pantalla.setCurrent(listas[0]);
    }if (c == back){ //Selecciono comando “Atrás”

    pantalla.setCurrent(listas[0]);
    }

    else if (d==listas[0]){
    System.err.println("Entro en la lista 0: \n");
    if(c==listas[0].SELECT_COMMAND){// Si selecciono
    switch(listas[0].getSelectedIndex()){ //opcion del menu
    case 2:{ pantalla.setCurrent(listas[1]);break;}
    case 1:{ pantalla.setCurrent(listas[2]);break;}
    case 0:{pantalla.setCurrent(listas[3]);break;} // pantalla.setCurrent(listas[3]);break;
    }
    }
    }else if(d==listas[1]){
    System.err.println("Entro en la lista 1: \n");
    if(c==List.SELECT_COMMAND){
    int i=listas[1].getSelectedIndex();
    switch(i){ //opcion del menu
    case 1:{ canvas = new mostrarfotos(fotos[0]);
    canvas.addCommand(exit);
    canvas.addCommand(back);
    canvas.setCommandListener(this) ;
    pantalla.setCurrent(canvas);}
    case 0:{canvas = new mostrarfotos(fotos[1]);
    canvas.addCommand(exit);
    canvas.addCommand(back);
    canvas.setCommandListener(this) ;
    pantalla.setCurrent(canvas);}
    }
    }
    }else if(d==listas[2]){
    System.err.println("Entro en la lista 2: \n");
    if(c==List.SELECT_COMMAND){
    int i=listas[2].getSelectedIndex();
    switch(i){ //opcion del menu
    case 1:{ canvas = new mostrarfotos(fotos[3]);
    canvas.addCommand(exit);
    canvas.addCommand(back);
    canvas.setCommandListener(this) ;
    pantalla.setCurrent(canvas);}
    case 0:{canvas = new mostrarfotos(fotos[2]);
    canvas.addCommand(exit);
    canvas.addCommand(back);
    canvas.setCommandListener(this) ;
    pantalla.setCurrent(canvas);}
    }
    }
    }else if(d==listas[3]){
    System.err.println("Entro en la lista 3: \n");
    if(c==List.SELECT_COMMAND){
    int i=listas[3].getSelectedIndex();
    switch(i){ //opcion del menu
    case 1:{ canvas = new mostrarfotos(fotos[5]);
    canvas.addCommand(exit);
    canvas.addCommand(back);
    canvas.setCommandListener(this) ;
    pantalla.setCurrent(canvas);}
    case 0:{canvas = new mostrarfotos(fotos[4]);
    canvas.addCommand(exit);
    canvas.addCommand(back);
    canvas.setCommandListener(this) ;
    pantalla.setCurrent(canvas);}
    }
    }
    }


    if (c == back){ //Selecciono comando “Atrás”

    pantalla.setCurrent(listas[0]);
    }
    }

    Where Listas[0] contains the main list, Listas[1] contains "Photos Russia", Listas[2] contains "Photos Spain" and so on.
    But it does not work, and I do not know why. Can somebody help me??

  2. #2
    pegitha is offline Member
    Join Date
    Apr 2007
    Location
    Indiana
    Posts
    83
    Rep Power
    0

    Default

    Well, I have a few questions.
    first of all do you know you have this code twice:
    Java Code:
    if (c == back){ //Selecciono comando “Atrás”
    pantalla.setCurrent(listas[0]);
    }if (c == back){ //Selecciono comando “Atrás”
    pantalla.setCurrent(listas[0]);
    }
    Exactly what Command package are you using? I see you are using the "==" instead of ".equals" .
    Just two thoughts right off the top of my head without knowing your imports.
    p

  3. #3
    luisarca is offline Member
    Join Date
    May 2007
    Posts
    11
    Rep Power
    0

    Default Thank you very much

    First of all, thank you very much for your reply.
    Yes, I know that I have twice the command back, but there is not important in this case. I think it is not my problem.

    The second question, .equals. I know that it is better to use .equals, but in a program that I did before I used .equals instead of (==) and the result was the same... But if you think I should change it, I will do, but I dont know if it is going to solve my problem.

  4. #4
    pegitha is offline Member
    Join Date
    Apr 2007
    Location
    Indiana
    Posts
    83
    Rep Power
    0

    Default

    exactly what is the package that the Command class is coming from? Can you post your imports please? Thanks

  5. #5
    pegitha is offline Member
    Join Date
    Apr 2007
    Location
    Indiana
    Posts
    83
    Rep Power
    0

    Default

    Another issue with the code, each case is supposed to always have "break;" as the last liine.
    case 1: canvas = new mostrarfotos(fotos[5]);
    canvas.addCommand(exit);
    canvas.addCommand(back);
    canvas.setCommandListener(this) ;
    pantalla.setCurrent(canvas);
    break;
    I have never used curley braces with the case either as you seem to have in your code. Mine look like this:
    Java Code:
    switch(i){
    case o:
      doThis();
      break;
    case 1:
      doThat();
      break;
    }
    curley braces only around the switch

  6. #6
    luisarca is offline Member
    Join Date
    May 2007
    Posts
    11
    Rep Power
    0

    Default It Works!

    Thank you very much, Pegitha. It works, the program works. As you could have seen I am not an expert in J2ME.
    Thank you very much for your help.

Similar Threads

  1. session handling
    By priyanka_t in forum JavaServer Pages (JSP) and JSTL
    Replies: 10
    Last Post: 08-11-2009, 12:32 PM
  2. Event
    By nt5515 in forum New To Java
    Replies: 0
    Last Post: 02-15-2008, 04:44 PM
  3. SWT Event Handling
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-30-2007, 12:21 PM
  4. Combo Box Event handling
    By smajidali26 in forum AWT / Swing
    Replies: 1
    Last Post: 11-29-2007, 05:57 PM
  5. problem with event handling!!!
    By ahdus in forum Java Applets
    Replies: 1
    Last Post: 11-17-2007, 06:24 PM

Posting Permissions

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