Link to home
Start Free TrialLog in
Avatar of rhithoun
rhithoun

asked on

How to unselect the radio button ?

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 ?
Avatar of Louis LIETAER
Louis LIETAER
Flag of France image

Hi,

Intrinsecly a group of radio button has a one and only selected button. I don't think you can reach your goal esealy.

There is a work around for your example add a button "unknown"

Avatar of rhithoun
rhithoun

ASKER

I need initially unselected options ...After that i can select either one option.
As I told you, a group of radio buttons will have a default value.
In your example male of female or with a third unknown (it may be invisible, just think about it).
I believe you are using a List for the radio buttons. You can get the desired result by using a ChoiceGroup insted of the List. Please find a sample code below.  You can add commands to the form and do the required checks in the commandAction. Remember that the choiceType should be MULTIPLE in case of EXCLUSIVE one will be selected by default.
ChoiceGroup choiceGroup = new ChoiceGroup("CH", ChoiceGroup.MULTIPLE);
choiceGroup.append("CG1", null);
choiceGroup.append("CG2", null);
choiceGroup.setSelectedIndex(0, false);
choiceGroup.setSelectedIndex(1, false);
form.append(choiceGroup);

Open in new window

The above solution also works for List. Only things is the check for only one option being selected is to be done in the commandAction.
ChoiceGroup choiceGroup = new ChoiceGroup("CH", ChoiceGroup.EXCLUSIVE);

Is this possible in choicegroup option EXCLUSIVE ...
I need like this radio buttons on MIDlet programming
sample.doc
Thank you answering my questions....
I need ChoiceGroup.EXCLUSIVE - default all radio buttons unselect.
But your answer the options for checkboxes(ChoiceGroup.MULTIPLE);

My requirment is open page defau;lt all radio buttons unselect(like refer the document)...So , please give solutions for ChoiceGroup.EXCLUSIVE .
I do understand that you need a solution for ChoiceGroup.EXCLUSIVE, but using that either of the options will be selected by default. You can't have all options unselected by default. So I suggested using ChoiceGroup.MULTIPLE and do the checks for EXCLUSIVE Selections in the commandAction code. The appearance will change for sure.
Another option is to use a Custom Item which can be made to behave in whatever way possible.
Here is an example.

http://www.ibm.com/developerworks/wireless/library/wi-developui/

Modify the code according to your requirement.
Hello, Just repost my solution

As I told you, a group of radio buttons will have a default value.

Make a group of 3 radio buttons male, female,unknow and set unknow as default and to invisible.
Please suggest , how to invisible the radio buttons ? ....
Set visible radiobutton property to false (if it is published)
There are no invisible radio buttons in J2ME.
You will have to use a Custom Item
Thank you for your responses..
custom item support EXCLUSIVE  choice group ?
ASKER CERTIFIED SOLUTION
Avatar of yogeshpmangrulkar
yogeshpmangrulkar
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yogesh , its very complicated coding level, so please help with

ChoiceGroup choiceGroup = new ChoiceGroup("CH", ChoiceGroup.MULTIPLE);
choiceGroup.append("CG1", null);
choiceGroup.append("CG2", null);
choiceGroup.setSelectedIndex(0, false);
choiceGroup.setSelectedIndex(1, false);
form.append(choiceGroup);
Code..

Its not working for me..
ChoiceGroup choiceGroup = new ChoiceGroup("CH", ChoiceGroup.MULTIPLE);
choiceGroup.append("CG1", null);
choiceGroup.append("CG2", null);
choiceGroup.setSelectedIndex(0, false);
choiceGroup.setSelectedIndex(1, false);
form.append(choiceGroup);

But this multiple choicegroup selecting all options.How to select only one choicegroup and initialy no one choice groupselected.
All the options can be made unselected at the beginning by setting setSelectedIndex(n,false);
n from 0 to number of options. The user gets a option to select multiple options at the same time. Now you can add a check either in the commandAction to ensure that only one option is selected. I'll post a sample code in sometime.
Suppose that you have added a Command Next to your from and your then add this code to your Command Listener  
public void commandAction(Command cmd, Displayable arg1)
   {
      if (cmd.equals(next))
      {
         boolean[] flags = new boolean[2];
         choiceGroup.getSelectedFlags(flags);
         // check for all the flags here, if more than one are 
         // true display the required message to 
         // the user and let him select again.
      }
    }

Open in new window

Checkboxes option but radio button functionalities ...is it possible ?
Check box is ok, you will have to write some code to uncheck the one that is not the last select. As I wrote in my first message only a work arround could be apllied for radio buttons. To make  third one invisible have tried to put an another componemt on the top of it, like a small grey panel or an image. The third button will undernice and invisible  

Hi,

Putting a panel or an image is not an option as in J2ME you cannot overlap the items in the j2me form.

Yes that is a good point here. You can add code to uncheck other other options when one is selected. The code has to be put into the itemStateChanged() function make your call implement the ItemStateListener interface and then implement the method. But the state change method is called at different times on different phones. It may not behave perfectly the same on all phones. So its better to ensure that only one option s selected in the commandAction().

ChoiceGroup.MULTIPLE retun -1 value ...There is no index value for that ...Is it possible for 0,1,2,..like that..

All check boxes returning -1 value only..
Use getSelectedFlags(boolean[] flags).

http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/lcdui/ChoiceGroup.html

Queries the state of a ChoiceGroup and returns the state of all elements in the boolean array flags return. Note: this is a result parameter. It must be at least as long as the size of the ChoiceGroup as returned by size(). If the array is longer, the extra elements are set to false.

For ChoiceGroup objects of type MULTIPLE, any number of elements may be selected and set to true in the result array. For ChoiceGroup objects of type EXCLUSIVE and POPUP exactly one element will be selected, unless there are zero elements in the ChoiceGroup.
I didn't get proper solution , so please close this question..