• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Understanding Events

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the response Gregg. Your answer made perfect sense and was just what I needed.

Landon
 
them good ole boys were drinking whiskey and rye singin' this'll be the day that I die. Drink tiny ad.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic