• Post Reply Bookmark Topic Watch Topic
  • New Topic

JRadio Button : Event Listener

 
yuvaraj vanarase
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
we can add actionlistener as well as changelistener for JRadioButton...
which should be preferred and why..?

For me i want to disable some components when button gets selected and disable whn deselected.

Thanks.
 
Campbell Ritchie
Marshal
Posts: 52549
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look up ChangeListener and ActionListener on the API.

If you click a button, it fires an ActionEvent.
If you have your radio buttons in a JButtonGroup, all the buttons in that group will change their state. Does that help?

CR
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a fan of another listener for this case: ItemListener. It is notified when what it is listening to is selected or deselected. This is true even if the change is programmatic -- done without a user clicking the mouse. To choose between ItemListener and ActionListener ask yourself what you really what to react to -- a change in selection or literally the mouse click.

And a big problem with ChangeListener is that it will fire for other state changes -- just wiggle the mouse over the radio buttons in my demo!

Demo:


Perhaps the real lesson here is that if you don't have a good textbook (and is there one for Swing?), you should be ready to whip up test programs. Don't delete them, because pretty soon you will have a directory full of them!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!