Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Understanding Events  RSS feed

 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to get a handle on the event mechanisms used in Java Beans and Swing. I know that all event sources pass an event object to the event listener when they call the event listener method. I am wondering what the purpose of this object is. The online documentation from Sun said that this event object provides a useful method, Object getsource(), which returns a reference to the object that was the source of the event. I'm trying to figure out why you would need this feature. Does the following situation illustrate this?

I have an object called MyButtonListener that is registered as an event listener on two different buttons, GoButton and StopButton. When an event is sent from GoButton, I want one action performed, when an event from Stop Button is sent, I want another action performed. Would both events call the same method in MyButtonListener, since thay are both buttons?

If they did, would you query the event object sent from the button, and use the result in an if/else statement to call the correct method?

I hope this makes sense. Any information is gratly appreciated.

Thanks,

Landon
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if I can explain the entire event mechanisms used by the awt.event api but I can explain a bit about what good the getSource method of the event object is.

Example 1:


In the above code, you only require 1 actionPerformed() method. The e.getSource() will help you determine which button was clicked so you can perform the correct action. You pretty much already stated this.

The other option, and the one I prefer, is to code it like this...



This difference here being that a new ActionListener object is created for every single button. I like this simply because it makes more sense to me and I don't have a single method full of if else's. However, the drawback to this is you create a lot more objects in memory.

The only time I really use a single object and implmenet the action on the class is for things like MouseListeners, FocusListeners. Basically listeners that it usually doesn't matter which component the action is on, but the same action occurs regardless. For example, I created an application one time where I needed JTextFields highlighted when they gained focus. So I implemented a FocusListener on my class. Then on focusGained I would highlight whatever JTextField I had. I still had to use getSource because I needed the instance of the JTextField object I had focus on, but I didn't care about anything beyong that. No if else's.

I hope I didn't confuse you more by this post.
 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response Gregg. Your answer made perfect sense and was just what I needed.

Landon
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!