 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jeffrey Spoon Guest
|
Posted: Sun Nov 20, 2005 3:09 pm Post subject: AWT custom events problem |
|
|
Hello, I'm trying to figure out AWT custom events.
I saw the code below in googlegroups.
My question is, what class should AClass represent? I'm not sure what
class to pass here and I don't understand what obj is passed when firing
your event (in the doSomething method).
Cheers
<http://groups.google.com/group/comp.lang.java.help/browse_frm/thread/7e6
b6a6c9e100e93/d4492375361b6d0a?lnk=st&q=java+custom+events+and+listeners&
rnum=1#d4492375361b6d0a>
----- First, create an interface that all your event listeners should
implement.
import java.util.EventListener;
public interface YourListener extends EventListener
{
public void yourMethod1(YourEvent evt);
public void yourMethod2(YourEvent evt);
}
------ Then, create YourEvent class :
import java.awt.AWTEvent;
public class YourEvent extends AWTEvent {
public YourEvent(AClass obj)
super(obj, YOUR_EVENT);
}
// Give your event a unique eventID
public static final int YOUR_EVENT = java.awt.AWTEvent.RESERVED_ID_MAX
+
1234;
}
------ Next, in the class where you want to fire the custom events.
Add
the following lines:
import javax.swing.event.EventListenerList;
public class YourCustomEventGeneratorClass
{
...
private EventListenerList listenerList = new EventListenerList();;
private YourEvent yourEvent = null;
...
public void doSomething()
{ ...
fireYourEvent(obj);
...
}
/** Adds a event listener to this object */
public void addYourListener(YourListener l)
{
listenerList.add(YourListener.class, l);
}
/** Removes a event listener from this object */
public void removeYourListener(YourListener l)
{
listenerList.remove(YourListener.class, l);
}
/** Notify all listeners that have registered interest for notification
when an
object gets.
*/
protected void fireYourEvent(AClass obj)
{
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2)
{
if (listeners[i]==YourListener.class)
{
// Lazily create the event:
if (yourEvent == null)
yourEvent = new YourEvent(obj);
((YourListener)listeners[i+1]).yourMethod1(yourEvent);
}
}
} // end fire
} // end class
------ Finally, implement the interface in any class you want.
public class YourEventTargetClass implements YourListener
{
...
private YourCustomEventGeneratorClass yourClass = new
YourCustomEventGeneratorClass();
yourClass.addYourListener(this);
...
public void yourMethod1(YourEvent evt)
{
// Add your implementation here
}
public void yourMethod2(YourEvent evt)
{
// Add your implementation here
}
}
--
Jeffrey Spoon
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Sun Nov 20, 2005 3:43 pm Post subject: Re: AWT custom events problem |
|
|
On Sun, 20 Nov 2005 15:09:24 +0000, Jeffrey Spoon
<JeffreySpoon (AT) hotmail (DOT) com> wrote, quoted or indirectly quoted someone
who said :
| Quote: | protected void fireYourEvent(AClass obj)
...
yourEvent = new YourEvent(obj);
|
It is whatever information your Event piggybacks.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
|
|
| Back to top |
|
 |
Jeffrey Spoon Guest
|
Posted: Sun Nov 20, 2005 5:03 pm Post subject: Re: AWT custom events problem |
|
|
In message <43809e58$0$1456$ed2619ec (AT) ptn-nntp-reader01 (DOT) plus.net>, Thomas
Hawtin <usenet (AT) tackline (DOT) plus.com> writes
| Quote: | yourEvent = new YourEvent(obj);
It is whatever information your Event piggybacks.
Actually, it is the source of the event. It's passed as the source
argument to the superclass constructor:
[url]http://java.sun.com/j2se/1.5.0/docs/api/java/awt/AWTEvent.html#AWTEvent([/url]
java.lang.Object, int)
source would have made a better variable name than obj.
Any additional information on the event should be passed as additional
arguments and get methods supplied.
Tom Hawtin
|
Thanks for the replies.
Excuse my thickness, but the source is presumably an instance of the
class which fires the event (in this case an instance of
YourCustomEventGeneratorClass)?
--
Jeffrey Spoon
|
|
| Back to top |
|
 |
Jeffrey Spoon Guest
|
Posted: Mon Nov 21, 2005 1:42 pm Post subject: Re: AWT custom events problem |
|
|
In message <4380b85f$0$1466$ed2619ec (AT) ptn-nntp-reader01 (DOT) plus.net>, Thomas
Hawtin <usenet (AT) tackline (DOT) plus.com> writes
| Quote: | The source should be the object that the event listener expects to receive.
You may have levels of indirection. For instance, if my code added a
listener to a button, but the button implementation delegates event
firing to another class, I would still expect the button to be the
source.
Similarly if I wrote model that delegated to another of the same
interface, I'd rewrite the events so that client code would see my
model as the source. If you look at the source to
javax.swing.AbstractButton, you will see it adding its own action
listener to the ButtonModel and maintaining its own list of listeners.
It doesn't just add client listeners straight onto the model.
Tom Hawtin
|
Thanks Tom, I got that working nicely.
Cheers
--
Jeffrey Spoon
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|