This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Swing class and events Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Swing class and events" Watch "Swing class and events" New topic
Author

Swing class and events

Phil Hopgood
Ranch Hand

Joined: Jul 14, 2008
Posts: 47
Hi All,

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.

Thanks for your help.

Regards,
Phil
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8839
    
    7

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

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.


Steve
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

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.
Phil Hopgood
Ranch Hand

Joined: Jul 14, 2008
Posts: 47
AH HA!

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!

Thanks guys, have a good weekend.

Regards,
Phil
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Well it's not that obvious. These are the direct listeners, but what about the listeners for the models? ListModel can also have ListDataListeners added.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
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!
Phil Hopgood
Ranch Hand

Joined: Jul 14, 2008
Posts: 47
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'"

Regards,
Phil.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swing class and events
 
Similar Threads
Status Bar Design Help
Java Virtual Machine troubleshooting
JComboBox
InputEvent ...in event listener model
beguinning mediator stuff