*
The moose likes Beginning Java and the fly likes enableEvents() .... confused ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "enableEvents() .... confused !" Watch "enableEvents() .... confused !" New topic
Author

enableEvents() .... confused !

Jennifer Wallace
Ranch Hand

Joined: Nov 30, 2001
Posts: 102
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

Joined: May 11, 2001
Posts: 152
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

Joined: Sep 29, 2000
Posts: 8521
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 ]

"JavaRanch, where the deer and the Certified play" - David O'Meara
Jennifer Wallace
Ranch Hand

Joined: Nov 30, 2001
Posts: 102
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: enableEvents() .... confused !