Hi all, I started to look into event handlers, and there is something I would like to ask, sorry if this is a silly one. I can see that different Java components generate different events when the user interacts with them, but when it comes down to 1)which interface the class that represents the event handler implements (ActionListener,ItemListener etc) and 2) what type of event is generated (ActionEvent, ItemEvent etc), how do I know which one to use? I mean, so far I know that if I want to implement event handling for, say, a textField, the inner class (or anonymous one which I haven't looked into as yet) implements the ActionListener interface and that the event passed on to the class method is and ActionEvent. But how do I know what each java component does? "API" I hear you saying, and well, "To an extent" I'd reply. If I have a look in the API for textField I have a list as long as my arm of methods, classes, etc etc http://docs.oracle.com/javase/7/docs/api/java/awt/TextField.html so how do I actually find what I want, as in which event does it trigger, how do I register the event handler? Or is it something that with time I will just know?
thanks
Recommended Answers
Jump to PostSorry, but yes, the answer is in the API doc!
Just look for the addXxxListener methods (being alphbetical, they're conveniently near the beginnning of the summary list). Dopn't forget to look at the methods inherited from superclasse as well.
Jump to PostOr is it something that with time I will just know?
-yes you have to accept that some XxxListener firing only one events, another two
Jump to PostIn the API doc look for adddXXXListener methods. Don't forget to check all the methods in the "Methods inherited from class XXX" sections.
eg
JButton has no such methods of it own, but it inheritsaddActionListener, addChangeListener, addItemListener from
AbstractButton
,addAncestorListener, addVetoableChangeListener from
JComponent
,addContainerListener, addPropertyChangeListener from
Container
,addComponentListener, addFocusListener, …
All 11 Replies
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.