• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Event Handling

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
my question is
Which of the below methods will help you identify which component was affected by ActionEvent?
1> getSource()
2> getComponent()

Which is the correct answer. According to me both should work but if its asked in the exam which should be choosen & why?
Thanks
Kiran Shetty
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kiran
It should be getSource(). According to the API:
public Object getSource()
The object on which the Event initially occurred.
What is getComponent() and what class is it in? EventObject inherits directly from Object and neither of those classes have a getComponent() method in them. Is is in one of the events that inherit from EventObject? If that is the case it is probaly a specialized method for that particular event. If you get a general questions on event handling then go with the getSource() method.
hth
Dave
 
Shetty
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dave,

Thanks for ur suggestion. Well I forgot to mention
getComponent() belongs to ComponentEvent class & it returns the Component object that originated the event.
If the question is asked as below
Which of the below methods will help you identify which component was affected by ActionEvent?
getComponent()
getSource()
Do u think getComponent() would be a better option as it is returning the Component Object or u still feel that the question is generic & getSource() is a better option.
Thanks,
Kiran Shetty.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shetty,
ComponentEvent is not a superclass of ActionEvent; they both derive from AWTEvent, which has no getComponent() method.
Therefore, the getComponent() method is not applicable to events generated by an ActionEvent.
From the API:

Component events are provided for notification purposes ONLY; The AWT will automatically handle component moves and resizes
internally so that GUI layout works properly regardless of whether a program is receiving these events or not.

You must always use getSource() to isolate the sources of ActionEvents.
 
Shetty
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Scott,
Thanks for ur suggestion. My doubt is cleared.
Kiran Shetty
 
I am displeased. You are no longer allowed to read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic