Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

javax.swing.ButtonGroup.getSelection()

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
" 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.
 
Wim Vanni
Ranch Hand
Posts: 96
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it me or is the code already following the requirement?

Wim
 
Campbell Ritchie
Sheriff
Pie
Posts: 50212
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This discussion would fit better in our GUIs forum. Moving.
 
Rob Camick
Ranch Hand
Posts: 2616
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out Darryl Burke's Select Button Group.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic