I have two JRadioButtons grouped, so only one of them can be selected.
How do I setup so the selected JRadioButton can not be selected again?
Regards
Martin
> I have two JRadioButtons grouped, so only one of them can be selected.
>
[quoted text clipped - 3 lines]
>
> Martin
Hi Martin,
I guess it depends what you're trying to accomplish: at one extreme, you
could just disable the button when it's been selected. Another possible
solution would be to add a proxy class that listens to and represents the
radio buttons. Objects that want to listen for events on the buttons would
register as ActionListeners with the proxy instead. The proxy would have to
"remember" what button was last selected, and if that button is selected
again, the proxy can just suppress the event so nothing happens.
In the latter case, your proxy class would have to implement event
propagation in the same way that other components do; it'll need a list of
event listeners (see e.g. javax.swing.EventListenerList), and will have to
implement ActionListener. In the actionPerformed method, it tests whether
the new command (or source, or event id; whatever) is the same as the last
one fired. If so, do nothing. If not, then fireActionPeformed (you'll
probably have to write this method) to all registered ActionListeners.
The skeleton of the class would look something like:
public class RadioButtonProxy extends ButtonGroup implements ActionListener
{
private javax.swing.EventListenerList listeners;
// override these methods from ButtonGroup
// so they add a call to button.{add | remove}ActionListener(this)
public void add(AbstractButton button);
public void remove(AbstractButton button);
public void actionPerformed(ActionEvent e);
public void addActionListener(ActionListener listener);
public void removeActionListener(ActionListener listener);
protected void fireActionPerformed(ActionEvent e);
}
Doing this by implementing the proxy class has the added benefit of allowing
for simplified button management -- for example if you require the ability
to deselect all radios you can do it using a dummy button hidden in the
proxy.
Hope this points you in a useful direction!
Sean.
P.S. It's possible that just adding the radios to a ButtonGroup will
accomplish what you want, if you haven't already done that.