File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes javax.swing.ButtonGroup.getSelection() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "javax.swing.ButtonGroup.getSelection()" Watch "javax.swing.ButtonGroup.getSelection()" New topic
Author

javax.swing.ButtonGroup.getSelection()

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

" If you have a group of radio buttons, you know that only one of them is selected. It
would be nice to be able to quickly find out which one without having to query all the but-
tons in the group. Because the ButtonGroup object controls all buttons, it would be conve-
nient if this object could give us a reference to the selected button. Indeed, the ButtonGroup
class has a getSelection method, but that method doesn’t return the radio button that is
selected. Instead, it returns a ButtonModel reference to the model attached to the button.
Unfortunately, none of the ButtonModel methods are very helpful. The ButtonModel interface
inherits a method getSelectedObjects from the ItemSelectable interface that, rather uselessly,
returns null. The getActionCommand method looks promising because the “action command”
of a radio button is its text label. But the action command of its model is null.

Only if you explicitly set the action commands of all radio buttons with the setActionCommand method do
the models’ action command values also get set. Then you can retrieve the action command
of the currently selected button with buttonGroup.getSelection().getActionCommand()"

Core Java Volume I (8th Edition) P.389

How would you adapt the following code snippet to read the action command, and, switch between different options?

The code



Related links:
1. http://download.oracle.com/javase/6/docs/api/javax/swing/ButtonGroup.html.

Jon
Wim Vanni
Ranch Hand

Joined: Apr 06, 2011
Posts: 96

Is it me or is the code already following the requirement?

Wim
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
This discussion would fit better in our GUIs forum. Moving.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2214
    
    7
Check out Darryl Burke's Select Button Group.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

Rob, thanks for the plug, but it wouldn't apply for the stated assignment.

Jon Camilleri wrote:How would you adapt the following code snippet to read the action command, and, switch between different options?


This appears to be homework.


luck, db
There are no new questions, but there may be new answers.
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Darryl Burke wrote:Rob, thanks for the plug, but it wouldn't apply for the stated assignment.

Jon Camilleri wrote:How would you adapt the following code snippet to read the action command, and, switch between different options?


This appears to be homework.


Not really it's just a question..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: javax.swing.ButtonGroup.getSelection()