Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Event handling  RSS feed

 
roshni bhagat
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when we talk of GUI,we say that whenever a user performs any action i.e presses - releases a button or press any key or mouse motion,it is an event which is represented in java as event object. I am confused about how to classify these events, whether it's an ActionEvent or ItemEvent or WindowEvent,MouseEvent...etc..etc???
when we click on 'button' component, it generates Actionevent and we implement ActionListener interface, but if we click on checkbox ,we implement ItemListener since it generates ItemEvent, why so? I'm so confused...!!!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
many components fire off numerous events.

if you click on a component it may fire off:
focusGained event
action event
mouse pressed event
mouse released event
mouse clicked event
etc
etc

you have to choose what you want to listen for.

eg you mention JCheckBox and ItemListener.
You could instead add an ActionListener and it will do pretty much the same thing,
but if you just want to know when the checkbox is selected, it's easier to use
ItemListener/ItemEvent/getStateChange() == ItemEvent.SELECTED
and you will only get the event where it has been selected, not for deselection
(and you can single out the deselection event as well).

and, you can also build all of this behavior into your actionListener code,
so you're probably more confused than before.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!