Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2003

Tip: Looking for answers? Try searching our database.

A quick questions concerning Radiobuttons

Thread view: 
Martin Siegumfeldt - 06 Dec 2003 21:19 GMT
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
Woebegone - 08 Dec 2003 04:29 GMT
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.