Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

enableEvents() .... confused !

 
Jennifer Wallace
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to understand enableEvents().
When the button is clicked,I expect the program to print ...
"From my process event"
"Button clicked"
But what I get is only ..
"From my process event"
1. What am I doing wrong ?
2. Is this a wrong/redundant way of trying to code enableEvents() ?

[ January 30, 2002: Message edited by: Cindy Glass ]
 
Joe Gilvary
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Applet is not registered to recieve the ActionEvents.
Change your init() to look like

and it should work.
HTH,
Joe
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use a listener - enableEvents is automatically turned on for you - so YES the calls to enableEvents are redundant. EITHER you use enableEvents OR you use the delegation model (Listeners) but not BOTH at the same time.
You should also take out the processEvents method and put all your stuff in the actionPerformed method.
In this case the applet itself is the listener - so when you make an instance of the applet you get an ActionListener created at the same time. That is why, as Joe said, when you b.addActionListener you use "this" as the thing to add.
[ January 30, 2002: Message edited by: Cindy Glass ]
 
Jennifer Wallace
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand how the event delegation model functions.
I got confused when I was studying
"Explicit Event Enabling" in RHE.
The author says,
Event Delegation is sufficient in most situations. However, there are times when it is preferable for a component to handle its own events, rather than delegating its events to listeners.

He has given an example ...
class MyBtn extends Button{
public MyBtn(String label){
super(label){
enableEvents(AWTEvent.ACTION_EVENT_MASK);
}
public void processActionEvent(ActionEvent ae){
System.out.println("Processign an action event");
super.processActionEvent(ae);
}
}
And also follows with an example where he subclasses Button and implements Actionlistener in the same class.(to achieve the same effect as above.)

Again, he says,

The only difference between this strategy and the enableEvents() startgey is the order in which event handlers are invoked.When u explicitly call enableEvents() the component's processActionEvent() method will be called before any Action Listeners are notified. When the component subclass is its own event listener, there is not guarantee as to order of notification.


Phew !! Now to my original question again....
What I was trying to do in my original post was
trying to code like what the author has done...
and also trying to get the Action listeners notified after I do the processEvent part in my program.
I knew registering the listeners wud automatically enable the events, so I didnt register them.
So how do i do it then ?
ie how do I see something happening in the
"super.processActionEvent()" call in the author's example.
I hope I have made my Question clear.
And, I am also trying to look into the tutorial for enableEvents(). Cudnt get to a direct hit in the "search" . Still looking...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic