Monday, December 10, 2012
Menempatkan beberapa objek ChoiceGroup dalam satu form, pada JAVA (J2ME)
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class DemoChoiceGroup extends MIDlet
implements CommandListener {
private Display display;
private Form form;
private TextField nama;
private ChoiceGroup jenisKelamin, hobi;
private Command cmdKeluar, cmdTampilkan;
public DemoChoiceGroup() {
display = Display.getDisplay(this);
form = new Form("Demo ChoiceGroup");
cmdKeluar = new Command("Keluar", Command.EXIT, 1);
form.addCommand(cmdKeluar);
cmdTampilkan = new Command("Tampilkan", Command.SCREEN, 1);
form.addCommand(cmdTampilkan);
form.setCommandListener(this);
}
public void startApp() {
nama = new TextField("Nama", null, 25, TextField.ANY);
form.append(nama);
String[] gender = {"Laki-laki","Perempuan"};
jenisKelamin = new ChoiceGroup("Jenis Kelamin",
Choice.EXCLUSIVE, gender, null);
form.append(jenisKelamin);
String[] hobbies = {"Membaca", "Memancing",
"Berolahraga", "Bersepeda"};
hobi = new ChoiceGroup("Hobi", Choice.MULTIPLE,
hobbies, null);
form.append(hobi);
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c == cmdKeluar) {
destroyApp(false);
notifyDestroyed();
} else {
Form newForm = new Form("Hasil");
StringItem siNama = new StringItem("Nama:\n",
nama.getString(), Item.PLAIN);
newForm.append(siNama);
StringItem siJenisKelamin =
new StringItem("Jenis kelamin:\n",
jenisKelamin.getString(jenisKelamin.getSelectedIndex()),
Item.PLAIN);
newForm.append(siJenisKelamin);
StringBuffer sb = new StringBuffer();
for (int i=0; i<hobi.size(); i++) {
if (hobi.isSelected(i)) {
sb.append("- " + hobi.getString(i) + "\n");
}
}
StringItem siHobi = new StringItem("Hobi:\n",
sb.toString(), Item.PLAIN);
newForm.append(siHobi);
display.setCurrent(newForm);
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment