[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1496
  • Last Modified:

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 ?
0
rhithoun
Asked:
rhithoun
  • 11
  • 10
  • 5
1 Solution
 
louislietaerCommented:
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"

0
 
rhithounAuthor Commented:
I need initially unselected options ...After that i can select either one option.
0
 
louislietaerCommented:
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).
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
yogeshpmangrulkarCommented:
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

0
 
yogeshpmangrulkarCommented:
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.
0
 
rhithounAuthor Commented:
ChoiceGroup choiceGroup = new ChoiceGroup("CH", ChoiceGroup.EXCLUSIVE);

Is this possible in choicegroup option EXCLUSIVE ...
0
 
rhithounAuthor Commented:
I need like this radio buttons on MIDlet programming
sample.doc
0
 
yogeshpmangrulkarCommented:
Yes it is possible. Check out the specs for details.
http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/lcdui/ChoiceGroup.html

Hear is a link for example.
http://www.java2s.com/Code/Java/J2ME/multipleChoiceGroup.htm

This will help for sure.
0
 
rhithounAuthor Commented:
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 .
0
 
yogeshpmangrulkarCommented:
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.
0
 
louislietaerCommented:
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.
0
 
rhithounAuthor Commented:
Please suggest , how to invisible the radio buttons ? ....
0
 
louislietaerCommented:
Set visible radiobutton property to false (if it is published)
0
 
yogeshpmangrulkarCommented:
There are no invisible radio buttons in J2ME.
You will have to use a Custom Item
0
 
rhithounAuthor Commented:
Thank you for your responses..
custom item support EXCLUSIVE  choice group ?
0
 
yogeshpmangrulkarCommented:
Custom Item are can be made to behave in any way you want them to.  Check this example. The coding part is a bit more complicated but it will give you the result in the way you want it. This is like making your own Choicegroup.

http://www.ibm.com/developerworks/wireless/library/wi-developui/
0
 
rhithounAuthor Commented:
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..
0
 
rhithounAuthor Commented:
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.
0
 
yogeshpmangrulkarCommented:
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.
0
 
yogeshpmangrulkarCommented:
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

0
 
rhithounAuthor Commented:
Checkboxes option but radio button functionalities ...is it possible ?
0
 
louislietaerCommented:
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  

0
 
yogeshpmangrulkarCommented:
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().

0
 
rhithounAuthor Commented:
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..
0
 
yogeshpmangrulkarCommented:
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.
0
 
rhithounAuthor Commented:
I didn't get proper solution , so please close this question..
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 11
  • 10
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now