• Post Reply Bookmark Topic Watch Topic
  • New Topic

ChangeListener stateChanged Question

 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a ChangeListener attached to a JRadioButton. The documentation is not clear (to me) on what events trigger the 'stateChanged' method. Based on the method name and what I gathered from the documentation, it should only trigger when the radio button is clicked. Apparently that is not the case. It seems to also be triggered when it gets focus and perhaps other events, unknown to me at this point. So my question is, how do I determine in 'stateChanged' specifically that the state changed? TIA.
 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javax.swing.event.ChangeListener is vague on purpose, because what constitutes the state of an object is very broad. I can't find anything in the documentation that would make me think it only triggers when the button is clicked. Focus, size and whether the component is enabled are also part of the component's state.

To get notified when a button is clicked by a user, you need to use java.awt.event.ActionListener.

 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I guess it was the name of the method that made me think it was sensible to use it for an on/off type component.
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
made me think it was sensible to use it for an on/off type component


You have been given a link to the Swing tutorial before for this exact reason.

The tutorial shows you the basics of how to use each component.

Read the tutorial and download the examples to learn the basics.

The API does not provide as much information as a working example can provide.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You misunderstand and incorrectly assume. It was not a question of HOW to use but rather WHEN. The examples did not clearly indicate what triggers a stateChanged event.
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but rather WHEN. 


And that is my point. If you look at the tutorial on radio buttons you will see that none of the examples use a ChangeListener.

So the question is why are you trying to do this? What problem are you trying to solve? First you need to learn the basics.

Then when the basics don't solve your problem you ask a question with a specific requirement.

There is no documentation for every situation that will cause a ChangeEvent to be generated. If you need to know then you need to learn how to answer the question yourself. The way you do this is to look at the source code and do a search for everywhere a ChangeEvent is generated.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!