Java Tutorial/J2ME/ChoiceGroup
Add choice to ChoiceGroup
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
public class J2MERadioButtons extends MIDlet implements ItemStateListener, CommandListener {
private Display display;
private Form form = new Form("");
private Command exit = new Command("Exit", Command.EXIT, 1);
private ChoiceGroup radioButtons = new ChoiceGroup("Select Your Color", Choice.EXCLUSIVE);
private int defaultIndex;
public J2MERadioButtons() {
display = Display.getDisplay(this);
radioButtons.append("Red", null);
radioButtons.append("White", null);
radioButtons.append("Blue", null);
radioButtons.append("Green", null);
defaultIndex = radioButtons.append("All", null);
radioButtons.setSelectedIndex(defaultIndex, true);
form.addCommand(exit);
form.setCommandListener(this);
form.setItemStateListener(this);
}
public void startApp() {
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command command, Displayable displayable) {
if (command == exit) {
destroyApp(true);
notifyDestroyed();
}
}
public void itemStateChanged(Item item) {
if (item == radioButtons) {
StringItem msg = new StringItem("Your color is ", radioButtons.getString(radioButtons.getSelectedIndex()));
form.append(msg);
}
}
}
Use ChoiceGroup
import java.io.IOException;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
public class ChoiceGroupMIDlet extends MIDlet {
protected Display display;
protected void startApp() {
display = Display.getDisplay(this);
Form form = new Form("Demo");
form.append("line");
try {
Image red = Image.createImage("/red.png");
Image green = Image.createImage("/green.png");
Image blue = Image.createImage("/blue.png");
String[] strings = new String[] { "Red", "Green", "Blue" };
Image[] images = new Image[] { red, green, blue };
ChoiceGroup exGroup = new ChoiceGroup("Choose one", ChoiceGroup.EXCLUSIVE,
strings, images);
form.append(exGroup);
ChoiceGroup multiGroup = new ChoiceGroup("Choose any", ChoiceGroup.MULTIPLE);
form.append(multiGroup);
multiGroup.append("Use SSL", null);
multiGroup.append("Reconnect on failure", null);
multiGroup.append("Enable tracing", null);
} catch (IOException ex) {
form.append("Failed to load images");
}
display.setCurrent(form);
}
protected void pauseApp() {
}
protected void destroyApp(boolean unconditional) {
}
}