1

I extended the Form class in lwuit and created a form class which has two commands, Next and Exit. Then I created a midlet to run that displays the form. The commands are being showed however nothing happens when they are clicked. Here is the code I wrote:

MainForm.java

import com.sun.lwuit.*;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.GridLayout;

public class MainForm extends Form implements ActionListener{
private Label label;
private RadioButton epl, laliga, seria, uefa, bundesliga;
private Command exit, next;
private String leagueName;
private ButtonGroup bg;
private TestMIDlet midlet;

public MainForm(TestMIDlet midlet){
    this.midlet = midlet;
    setTitle("Main Page");
    GridLayout gl = new GridLayout(6,1);
    setLayout(gl);
    label = new Label("Choose a league to proceed");
    epl = new RadioButton("EPL");
    laliga = new RadioButton("La liga");
    seria = new RadioButton("Seria A");
    bundesliga = new RadioButton("Bundesliga");
    uefa = new RadioButton("UEFA Champions League");
    uefa.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            leagueName = "International Clubs";
        }
    });
    bundesliga.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            leagueName = "Germany";
        }
    });
    seria.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            leagueName = "Italy";
        }
    });
    laliga.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            leagueName = "Spain";
        }
    });
    epl.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            leagueName = "England";
        }
    });
    bg = new ButtonGroup();
    bg.add(epl);
    bg.add(laliga);
    bg.add(seria);
    bg.add(bundesliga);
    bg.add(uefa);
    next = new Command("Next",2);
    exit = new Command("Exit", 2);
    addComponent(label);
    addComponent(epl);
    addComponent(laliga);
    addComponent(seria);
    addComponent(bundesliga);
    addComponent(uefa);
    addCommand(exit);
    addCommand(next);
}

public void actionPerformed(ActionEvent evt) {
    Command c = evt.getCommand();
    if (c == exit){
        midlet.destroyApp(false);
        midlet.notifyDestroyed();
    }
    else if (c == next){
            System.out.println(leagueName);
    }
}

}

xabush
  • 849
  • 1
  • 13
  • 29

2 Answers2

1

I reveiwed your total program and i found the solution for you. See here you implemented the ActionListener but you havent added the CommandListener to your Form. This is the cause that the commands havent been called when you clicked them . Follow the below Code and use it there.

this.addCommandListener(this);

Now everything works perfect in your code. Let me know if you face any other issue.

mr_lou
  • 1,910
  • 2
  • 14
  • 26
sri2377076
  • 78
  • 2
  • 9
  • Here i am saying add CommandListner to the Form . Then only it works . First let test this code. It works fine if we add the above code i suggested. – sri2377076 May 17 '14 at 06:12
  • `CommandListener` is used when you do LCDUI coding. `ActionListener` is used when you do LWUIT coding. The `ActionEvent` object you get with an `ActionListener` contains a `Command` object. There is nothing wrong with using `ActionListener`. I'd rather think it would be considered messy to use `CommandListener` in LWUIT coding. – mr_lou May 17 '14 at 07:21
  • here you can do like this , this.addCommandListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { } }); Here you can handle the Action Events. Where in the above, He implemented the ActionListener to his form . So i suggested him like that . Thanks . – sri2377076 May 17 '14 at 07:40
  • I took a closer look at LWUIT, and found that you're partly right. In order to `Command`s to be caught, you need to `setCommandListener(this);` so I have edited my answer. There is no `addCommandListener()` method though, as you are referring to. – mr_lou May 17 '14 at 10:53
  • Look more closer into the LWUIT you will find the addCommandListener to the FORM . Just extend com.sun.lwuit.Form . I am working in LWUIT and i will use the same approach . Thanks – sri2377076 May 17 '14 at 12:27
  • You're right. There is indeed an `addCommandListener` for the `Form` object. Earlier I looked at this LWUIT example I found: https://lwuit.java.net/tutorial/events.html It demonstrates using `setCommandListener`. I suppose both methods will work then. – mr_lou May 17 '14 at 12:41
  • 1
    @mr_lou the setCommandListener method was what was confusing me too.In the tutorials and books I read about lwuit, they all use the setCommandListener instead of addCommandListener.However,in the latest version of lwuit(v1.5) the method is replaced with addCommandListener.Anyways, thank you guys for your answer – xabush May 19 '14 at 10:06
  • Ah well, that explains it then. But you should still use `c.equals(exit)` instead of `c==exit` though. That part is equally important. ;-) – mr_lou May 19 '14 at 15:35
0

You cannot compare to objects using the equal sign. You have to use the Object.equals(anotherObject) method.

Replace your actionPerformed method with this:

public void actionPerformed(ActionEvent evt) {
 Command c = evt.getCommand();
 if (c.equals(exit)){
  midlet.destroyApp(false);
  midlet.notifyDestroyed();
 }
 else if (c.equals(next)) {
  System.out.println(leagueName);
 }
}

EDIT: Also, in order for your Commands to be caught, you need to call setCommandListener(this); Or addCommandListener(this);

mr_lou
  • 1,910
  • 2
  • 14
  • 26