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

Sorry, 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.

Here's a great tutorial
http://docs.oracle.com/javase/tutorial/uiswing/events/
and this page includes a table of whuch events go with which common classes
http://docs.oracle.com/javase/tutorial/uiswing/events/eventsandcomponents.html

Ps with Java 8 lambdas allow you to define a listener in a single concise expression http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#lambda-expressions-in-gui-applications

Or is it something that with time I will just know?

-yes you have to accept that some XxxListener firing only one events, another two

Thanks guys.
@JamesCherrill, thanks for the links I had seen at least one of those already :-). You mentioned Java 8 in another post already, but I think that for now I will stick to 7, I'd like to learn more (if I ever will) before upgrading and diving into a slightly different syntax

OK, that's your choice.
Personally I would question why anyone would spend time struggling to master the syntax of anonymous inner classes when they just became obsolete (unless they are working in an environment where Java 8 isn't allowed yet, or breaks existing applications), but hey ho.

uhm, well I can't argue with that...as I am going through anonymous classes right now...

ahm, sorry to resurrect an old thread, but it is relevant to what I am doing now. I want to find out what events a JButton supports. I am on the API page and hell, where do I find the type of events supported?! I know there is a tutorial here and I am looking at that too http://docs.oracle.com/javase/tutorial/uiswing/events/eventsandcomponents.html#all but if it wasn't for the table in the tutorial telling me which event is supported, I would be completely lost

In 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 inherits

addActionListener, addChangeListener, addItemListener from AbstractButton,

addAncestorListener, addVetoableChangeListener from JComponent,

addContainerListener, addPropertyChangeListener from Container,

addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener from Component

.. so that's the definitive list of all the listeners youcan add to a JButton.

  • most of JComponents has own model then for ButtonModel are accesible the same XxxListener as for AbstractButton

  • from ButtonModel (exclusivelly by using ChangeListener only) is possible to get all mouse & key events from/for/over JButton

right, thanks guys, sorry, I just finding it incredibly difficult to find my way around the API.

Yes, it's not easy to se the inherited members in particular. It would be great to have an (optional) mode in which all the available methods were listed (with parameters etc) together in one alphabetic listing.
Stick with it... working with the API JavaDoc is an essential Java skill, and it does get easier with practice.

Will try! thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.