Reputation: 877
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);
}
}
}
Upvotes: 1
Views: 155
Reputation: 78
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.
Upvotes: 1
Reputation: 1920
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 Command
s to be caught, you need to call setCommandListener(this);
Or addCommandListener(this);
Upvotes: 0