Search code examples
javajava-melwuit

Commands not working in LWUIT


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);
    }
}

}


Solution

  • 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.