This discussion is archived
4 Replies Latest reply: May 19, 2009 9:37 PM by 807581 RSS

urgent requirment in j2me

807581 Newbie
Currently Being Moderated
Hi ,

I have two radio buttons in j2me MIDlet application.
default one option selected.
but i want to defaultely unselected two options.

for example:

.male (unselect)
.female(unselect)

It it supported in j2me application ?

Thanks,
nambika.
  • 1. Re: urgent requirment in j2me
    807581 Newbie
    Currently Being Moderated
    Yes, this is accomplished using the ChoiceGroup class. Items added to the ChoiceGroup are deselected by default.
    For example:
    package test;
    
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    
    /**
     * @author Hans
     */
    public class ChoiceGroupMIDlet extends MIDlet implements CommandListener {
    
        private boolean midletPaused = false;
        private boolean boolMotion=false;
        Display mDisplay;
    
        Form frm;
    
        ChoiceGroup cg;
        Command cmd;
    
        /**
         * The ChoiceGroupMIDlet constructor.
         */
        public ChoiceGroupMIDlet() {
    
            frm=new Form("Choice Group Test");
            cg=new ChoiceGroup("Choose:",Choice.MULTIPLE);
            cmd=new Command("OK",Command.OK,1);
    
            cg.append("One",null);
            cg.append("Two",null);
            cg.append("Three",null);
            frm.append(cg);
            frm.setCommandListener(this);
        }
    
        /**
         * Called by a system to indicated that a command has been invoked on a particular displayable.
         * @param command the Command that was invoked
         * @param displayable the Displayable where the command was invoked
         */
        public void commandAction(Command command, Displayable displayable) {
           for(int i=0;i<3;i++) {
                if(cg.isSelected(i)) {
                    System.out.println("u selected :" +
                            cg.getString(i));
    
                    return;
                }
            }
        }
    
        
        /**
         * Called when MIDlet is started.
         * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
         */
        public void startApp() {
                mDisplay = Display.getDisplay(this);
                mDisplay.setCurrent(frm);
        }
    
        /**
         * Called when MIDlet is paused.
         */
        public void pauseApp() {
        }
    
        /**
         * Called to signal the MIDlet to terminate.
         * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
         */
        public void destroyApp(boolean unconditional) {
        }
    
    }
  • 2. Re: urgent requirment in j2me
    807581 Newbie
    Currently Being Moderated
    Thanks Hans ...But i need two checkboxes with initially unselected and mutually exclusive[like the radio button]
    The following code I wrote and got the solution.

    import javax.microedition.lcdui.Choice;
    import javax.microedition.lcdui.ChoiceGroup;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Form;
    import javax.microedition.lcdui.Item;
    import javax.microedition.lcdui.ItemStateListener;
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.midlet.MIDletStateChangeException;

    public class FunctionTest extends MIDlet implements ItemStateListener {
         private Display display ;
         private ChoiceGroup radioButtons;
         
         private Form form;
         boolean[] flag = new boolean[20];

         public FunctionTest() {
              // TODO Auto-generated constructor stub
         }

         protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
              // TODO Auto-generated method stub
         }

         protected void pauseApp() {
              // TODO Auto-generated method stub
         }

         protected void startApp() throws MIDletStateChangeException {
              display = Display.getDisplay(this);
              
              radioButtons = new ChoiceGroup(
                        "Select Your Color",
                        Choice.MULTIPLE);
                        radioButtons.append("Male", null);
                        radioButtons.append("Female", null);
              initFlag(radioButtons);
              
              form = new Form("");
              form.append(radioButtons);
              form.setItemStateListener(this);
              
              display.setCurrent(form );
         }
         
         public void itemStateChanged(Item item)
         {     
              if (item == radioButtons)
              {     
                   setExclusive(radioButtons);
              }
         }
         /*
         * This method used to set flag for the check boxes
         *
         */
         private void initFlag(ChoiceGroup item) {
              // Initially all flags are set to true
              for(int i=0;i< item.size();i++){
                   flag=true;
              }
         }
         /* method to set the check box as mutually exclusive
         *
         * This method applicable for number of check boxes
         */
         private void setExclusive(ChoiceGroup item) {
         for(int i=0;i< item.size();i++){
              if (item.isSelected(i) && flag[i] ){
                   form.append("inside for selected radioButtons.isSelected("+i+")"+item.isSelected(i));
                   for(int j=0;j< item.size();j++){
                        if (i!=j){
                             item.setSelectedIndex(j,false);
                             flag[j]=true;
                             flag[i]=false;
                        }}}}
         }
    }
  • 3. Re: urgent requirment in j2me
    807581 Newbie
    Currently Being Moderated
    import javax.microedition.lcdui.Choice;
    import javax.microedition.lcdui.ChoiceGroup;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Form;
    import javax.microedition.lcdui.Item;
    import javax.microedition.lcdui.ItemStateListener;
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.midlet.MIDletStateChangeException;
    
    public class FunctionTest extends MIDlet implements ItemStateListener {
         private Display display ;
         private ChoiceGroup radioButtons;
         
         private Form form;
         boolean[] flag = new  boolean[20];
         
         // boolean zeroFlag=true,oneFlag=true;
         public FunctionTest() {
              // TODO Auto-generated constructor stub
         }
    
         protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
              // TODO Auto-generated method stub
         }
    
         protected void pauseApp() {
              // TODO Auto-generated method stub
         }
    
         protected void startApp() throws MIDletStateChangeException {
              // TODO Auto-generated method stub
              display = Display.getDisplay(this);
              
              radioButtons = new ChoiceGroup(
                        "Select Your Color",
                        Choice.MULTIPLE);
                        radioButtons.append("Male", null);
                        radioButtons.append("Female", null);
              initFlag(radioButtons);
              
              form = new Form("");
              form.append(radioButtons);
                   
              form.setItemStateListener(this);
              
              display.setCurrent(form );
         }
         
         public void itemStateChanged(Item item)
         {     
              if (item == radioButtons)
              {     
                   callfunction(radioButtons);
              }
         }
         /*
          * This method used to set flag for the check boxes 
          * 
          */
         private void initFlag(ChoiceGroup item) {
              // Initially all flags are set to true
               for(int i=0;i< item.size();i++){
                    flag=true;
              }
         }
         /* method to set the check box as mutually exclusive
         *
         * This method applicable for number of check boxes
         */
         private void callfunction(ChoiceGroup item) {
         for(int i=0;i< item.size();i++){
              if (item.isSelected(i) && flag[i] ){
                   form.append("inside for selected radioButtons.isSelected("+i+")"+item.isSelected(i));
                   for(int j=0;j< item.size();j++){
                        if (i!=j){
                             item.setSelectedIndex(j,false);
                             flag[j]=true;
                             flag[i]=false;
                        }}}}
         }
    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
  • 4. Re: urgent requirment in j2me
    807581 Newbie
    Currently Being Moderated
    Thanks to Sun Forums