Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

awt!!!

 
sriram gupta
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2373
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good explanation Mathias...
------------------
azaman
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic