Home > Archive > Java Help > November 2005 > AWT custom events problem
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
AWT custom events problem
|
|
| Jeffrey Spoon 2005-11-20, 7:01 pm |
|
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..._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
| |
| Roedy Green 2005-11-20, 7:01 pm |
| On Sun, 20 Nov 2005 15:09:24 +0000, Jeffrey Spoon
<JeffreySpoon@hotmail.com> wrote, quoted or indirectly quoted someone
who said :
> 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.
| |
| Thomas Hawtin 2005-11-20, 7:01 pm |
| Roedy Green wrote:
> On Sun, 20 Nov 2005 15:09:24 +0000, Jeffrey Spoon
> <JeffreySpoon@hotmail.com> wrote, quoted or indirectly quoted someone
> who said :
>
>
>
> ..
>
>
> 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:
http://java.sun.com/j2se/1.5.0/docs...t.html#AWTEvent(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
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
| |
| Jeffrey Spoon 2005-11-20, 7:01 pm |
| In message <43809e58$0$1456$ed2619ec@ptn-nntp-reader01.plus.net>, Thomas
Hawtin <usenet@tackline.plus.com> writes
>
>Actually, it is the source of the event. It's passed as the source
>argument to the superclass constructor:
>
>http://java.sun.com/j2se/1.5.0/docs...t.html#AWTEvent(
>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
| |
| Thomas Hawtin 2005-11-20, 7:01 pm |
| Jeffrey Spoon wrote:
>
> Excuse my thickness, but the source is presumably an instance of the
> class which fires the event (in this case an instance of
> YourCustomEventGeneratorClass)?
Possibly.
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
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
| |
| Jeffrey Spoon 2005-11-21, 7:58 am |
| In message <4380b85f$0$1466$ed2619ec@ptn-nntp-reader01.plus.net>, Thomas
Hawtin <usenet@tackline.plus.com> writes
>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
|
|
|
|
|