This is a really dumb question ....... it's Friday afternoon!
How do I find out from the Sun API docs what event(s) a given swing class will fire? For example, I know JButton will fire an ActionEvent and thus I need to implement an ActionListener but what about JList, say? What events can JList fire off?
I've ploughed through the API documentation but I can't seem to find the event(s) that any given swing class is associated with. I tried looking from the other direction but without success. Take ComponentEvent for instance, how do I find which swing classes fire off a ComponentEvent?
I know the answer to this must be staring me in the face but I just wish it would wave at me! I'm gonna feel so ashamed when you tell me I just know it.
Off the top of my head (it's Friday here, ya know), I'd look at what addListener methods the class and it's superclasses have. Click on the name of the listener class and its methods will have the event class.
You have to look at a class' API and see what Listeners it can take. Then you can assume that the class will fire any and all of the events the given Listener can handle.
For example: JList has an 'addListSelectionListener' which assures you that it will use a ListSelectionListener, which happens to have a single method: valueChanged. If you read the method for addListSelectionListener it explains when the listener will get called.
Then look at the JList inherited methods: It gets addAncestorListener, and addVetoableChangeListener from JComponent, addContainerListener, addPropertyChangeListener from Container, and addComponentListener, addFocusListener, etc... from Component. All of these listeners then would get be applicable for the JList.
I do what Joe mentions - look at what addXyzListener methods exist for the class. And as Joe mentioned, the key is to pay attention to the super classes. For example, most of the addListener methods for a JButton are actually in the AbstractButton, JComponent, Container, and Component classes. Here's one place where an IDE's code complete feature is helpful.
I've never found a nice list or table that maps events to classes.
Joined: Jul 14, 2008
See, I knew it would be obvious. Just getting tangled up in all the inter-related links in the API I guess.
I spose I was just hoping that I could be lazy and find that little ole table mapping classes and events ....... dream on!
Find a copy of Thinking In Java by Bruce Eckel. The 3rd edition is probably still legally available free of charge on the Internet; it is easy to find on Google. Find the chapter about reflection, and see whether the example about Listeners is in it. That example (it's in my 4th edition) uses reflection to go through a Swing class and find all the addSomethingListener() methods it expresses. Copy it, compile it, run it, then copy the output into your table. Only the table will get quite large!
Joined: Jul 14, 2008
Thanks Rob, Campbell.
Campbell, that was a very good tip, you are a cool guy! I've now got a copy of TIJ 3rd edition in pdf and found the "ShowAddListeners" example in chapter 14 p812. I've not run it up yet but I'll do that later today.
It looks like a good book - I might even splash out and buy the 4th edition but to be honest I'm sinking under the weight of java books, articles, exercises, etc. that I think I need to read and absorb.
I think I'm just going to complete "Head First", then Migrate a small Powerbuilder GUI app I wrote a few years ago, then head on out on the cattle drive and "Keep them doggies movin'"