It's not a secret anymore!*
The moose likes Swing / AWT / SWT and the fly likes Selection of Radio button, when combo box is selected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Selection of Radio button, when combo box is selected" Watch "Selection of Radio button, when combo box is selected" New topic
Author

Selection of Radio button, when combo box is selected

Sophiya Behera
Greenhorn

Joined: May 10, 2012
Posts: 18
Hi all,
I am facing a issue in dynamic selection of radio button. I want to select a radio button at runtime and it depends on the selection of combobox. I have tried the following code
radiobutton.setSelected(true) and radiobutton.getmodel().setselected(true). But nothing is working. Please help me in this.


Regards
Sophiya
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

Are you sure that combo box selection event is triggering properly?


Swastik
Sophiya Behera
Greenhorn

Joined: May 10, 2012
Posts: 18
Hi Swastik,

Thnks for your reply.

Yes the event is triggered properly. I have debugged through each line. After setting setSelected(true) i checked for isSelected(), itis showing true. But in UI, the button is not selected. Could you please tell me any other example, if one simple button is clicked, how the radio button is selected.... That also will help me.



Regards
Sophiya
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

Hi Sophiya

You can have a look on this


Sophiya Behera
Greenhorn

Joined: May 10, 2012
Posts: 18
Swastik,
This example works. But my scenario is bit complex. I have created a panel and created group of radio buttons in that panel in a class. And i am trying to get hold of the radio button through button group. Please refer to below code. Please suggest...

Enumeration<AbstractButton> enumButtons;
String[] arrDfltVal = getDfltValArr();

for(int i=0;i<arr.length;i++){

enumButtons = arr[i].getBtnGroup().getElements();
while (enumButtons.hasMoreElements()){


//*AbstractButton ab= enumButtons.nextElement();
JRadioButton ab= (JRadioButton)enumButtons.nextElement();
//arr[i].getBtnGroup().setSelected(ab, true);


if ((ab.getName()).equalsIgnoreCase(arrDfltVal[i])){
System.out.println("isSelected"+ab.isSelected()+ab.getModel().isSelected());
//arr[i].getBtnGroup().setSelected(ab.getModel(), true);
ab.setSelected(true);
System.out.println("isSelected"+ab.isSelected()+ab.getModel().isSelected());

}

}


}
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

Hi Sophiya

It should be possible that you too. The code could have been better, but I did it in a hurry.

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> After setting setSelected(true) i checked for isSelected(), itis showing true. But in UI, the button is not selected.

quite likely you have duplicate declarations of your radioButtons
1) a class field referenced by the listener
2) a local variable where you add the radioButtons to the gui,
these are the radioButtons seen on the screen, but not 'seen' by the listener

would be much easier for you to get a solution if you posted a 'simple' working example
of the problem - just a frame, comboBox, 2 radioButtons and the listener code.
Sophiya Behera
Greenhorn

Joined: May 10, 2012
Posts: 18
Hi Swastik,
My issue is resolved. I have taken the same approach, but i was going wrong while accessing array of buttons. Everytime the values in the array was getting initialised. Thnks for your code.


Regards
Sophiya
Sophiya Behera
Greenhorn

Joined: May 10, 2012
Posts: 18
Hi Michael,
My issue is resolved and the reson what you have specified is quite similar to my issue. Thanks for your reply.


Regards
Sophiya
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

You are welcome.
 
Consider Paul's rocket mass heater.
 
subject: Selection of Radio button, when combo box is selected
 
Similar Threads
deselecting radio button
Radio button validation problem
Redrawing the UI
want to get the value of selected radio button
How can I disable a text input field?