[an error occurred while processing this directive]

J2ME CheckBox ChoiceGroup MIDlet

                         

This example illustrates how to create check boxes in to your form. In this example we are creating a Form("Technologies") that is having four different choices, ChoiceGroup("JAVA", "J2ME", "J2EE", "JSF"). if user select a check box and click on "Choose" button then the selected item will be display on the form. We are using StringItem to display the selected item on the form. We are creating a variable of array type ("message[ ]") and stored the size of selected item in it. Next we are creating a boolean type array ("get[ ]") to store the size of selected item then we have given a condition if get[] returns true then this will display the StringItem() with message and String value of selected item. The StringItem has only 2 methods that is:

  • getText()
  • setText(String text)

Constructor StringItem Syntax is:

  • StringItem(String label, String text) 

The Application look like as follows:

 

 

Source code of CheckBoxExample.java 

 
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;


public class CheckBoxExample extends MIDlet implements CommandListener {
  private Display display;
  private Form form;
  private Command exit, choose;
  private ChoiceGroup technology;
  private int index;

  public CheckBoxExample() {
    form = new Form("Technologies");
    technology = new ChoiceGroup("Select Technology Which You Know", Choice.MULTIPLE);
    exit = new Command("Exit", Command.EXIT, 1);
    choose = new Command("Choose", Command.SCREEN, 2);    
  }

  public void startApp() {
    display = Display.getDisplay(this);
    technology.append("JAVA"null);
    technology.append("J2ME"null);
    technology.append("J2EE"null);
    technology.append("JSF"null);



    index = form.append(technology);
    form.addCommand(exit);
    form.addCommand(choose);
    form.setCommandListener(this);
    display.setCurrent(form);
  }

  public void pauseApp(){}

  public void destroyApp(boolean unconditional){
    notifyDestroyed();
  }

  public void commandAction(Command c, Displayable displayable){
    String label = c.getLabel();
    if (label.equals("Choose")) {
      StringItem message[] new StringItem[technology.size()];
      boolean get[] new boolean[technology.size()];
      technology.getSelectedFlags(get);
      for (int i = 0; i < get.length; i++) {
        if (get[i]) {
          message[inew StringItem("Your Choice is: ", technology.getString(i));
          form.append(message[i]);
        }
      }
      form.delete(index);
      form.removeCommand(choose);
    else if (label.equals("Exit")){
      destroyApp(false);
    }
  }
}

 

Download Source Code

                         

[an error occurred while processing this directive]