The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes awt!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "awt!!!" Watch "awt!!!" New topic


sriram gupta
Ranch Hand

Joined: Aug 09, 2001
Posts: 39
there was a question in applied reasoning about
select each of the code fratgments that would allow a newly created java.awtButton to generate ActionEvents.
A button.enableActionEvents();
B button.addActionListener(anActionListener);
D button.enalbeEvents(AWTEvent.ACTION_EVENT_MASK);
i am not able to understand what does this question means..
and why is the option B is a correct answer..

thanks in advance...
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
You can provide a component a actionlistener with implementing a listener , extend a adapter. when you implementing an listener you must override all methods defined in the Listeners interface.
When extending an adapter you can just provide code for the action you like to perform.
There is an alternitive to delegating a component's events. It is possible to subclass the component and override the method that recives events and dispach them to listeners.
To do this you must use enableEvents( AWTEvent.X_EVENT_MASK )
And provide code for processXEvents( XEvent ae ) wher eX can be Action , Window etc.
Se the code

A. button.enableActionEvents();
B. button.addActionListener(anActionListener);
D. button.enalbeEvents(AWTEvent.ACTION_EVENT_MASK);

A is not sufficient. You must pass the static variable for AWTEvent class for ActioEvent. Plus the correct way to do it isn't enableActionEvents(); but enableEvents( AWTEvent.ACTION_EVENT_MASK );
D First enableEvents is wrongly spelled. But you must altso provide code for public void processActionEvents( ActionEvent e ) to pass it to the listener. D is altso wrong.
B is right.
Hope this helps
Christy Smith

Joined: Aug 15, 2001
Posts: 26
Is that true that in the delegation event model, the default events are enabled by default? It really is not necessary to invoke enableEvents()? Thanks
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Mr Smith is right. You can altso override the processEvent method.

By using this tecnique you dont have to use processActionEvent , processKeyEvent and so on. You can us processEvent( AWTEvent evt ) instead. The event return an id. You can get this id by using the getId() method of the AWTEvent class. You can make comparation to se what event got executed.
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2373

Good explanation Mathias...

Ashik Uzzaman
Senior Software Engineer, TubeMogul, Emeryville, CA, USA.
I agree. Here's the link:
subject: awt!!!
It's not a secret anymore!