subclassing event objects

Is there any purpose to creating subclasses of event objects such as EventObject or its subclasses. I thought that event objects are created at runtime to indicate events. Can events be created by an application? If so, please provide a simple example.
[273 byte] By [paul_sun] at [2007-9-26 1:14:58]
# 1

Yes, it's possible, and the source-listener architecture is a good design. Consider a component JSpin, a spin button that does not exist in swing. It provides events when a spin action is performed. The listner class can be:

public interface SpinListener extends EventListener

{

public void spinPerformed(SpinEvent event);

}

The spin event:

public class SpinEvent extends EventObject

{

public static final int UP_SPIN_PERFORMED = 0;

public static final int DOWN_SPIN_PERFORMED = 1;

private int id;

public SpinEvent(Object source, int id)

{

super(source);

this.id = id;

}

public int getId()

{

return id;

}

}

Actually, the implementation is yours to decide. Take two buttons on a panel, listen to some state changes or button acttions and then you can fire your own spin events to listeners. Thus you should implement these two methods:

public synchronized void addSpinListener(SpinListener listener)

public synchronized void removeSpinListener(SpinListener

listener)

And fire:

protected synchronized void fireSpinListenerSpinPerformed(

SpinEvent event)

{

Vector list;

if (spinListenerList != null)

{

list = (Vector) spinListenerList.clone();

for (int counter = 0; counter < list.size(); counter++)

{

((SpinListener) list.elementAt(counter)).

spinPerformed(event);

}

}

}

Kurta

h230561 at 2007-6-29 0:32:57 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2
Thanks for providing the sample code. However, I'm not clear as to the significance of subclassing the EventObject class. Can an event class be created that is not a subclass of EventObject that would work in your example? I don't see the reason for extending the EventObject class.
paul_sun at 2007-6-29 0:32:57 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 3

The reason is: to add your event specific knowledge to the event class. Of course you might define your own event class that is not a subclass of EventObject. For me it's just a convention, like implementing the empty interface EventListener.

To know more: http://developer.java.sun.com/developer/onlineTraining/Beans/JBShortCourse/beans.html

Kurta

h230561 at 2007-6-29 0:32:57 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 4

hi,

the purpose of subclassing is to customise/create/trigger ur own events upon occurance of an event which is not standard, such as in case of trigerring of an event when an spinner is of multiples of 5, the std. event here is that u can be notified during the change in the spin value, but if u need have an event that needs to be triggered of the values are in multiples of 5, u need to sub-class the event, so that the u can have both type of listeners. One, who will bw notified when the value changes, other when the value is in multiples of 5 or it may be a critical value, when a real time system is listener is listening for to take a appropriate step. The below method of the h230561 sample is the space where the above logic fits in to trigger the special event.

protected synchronized void fireSpinListenerSpinPerformed(SpinEvent event)

{

if(event.getId() != 0 && event.getId()%5 == 0)

{

// invoke the sub-classed myevent of

// type MyEvent which extends event.

fireMySpinListenerSpinPerformedmyevent);

}

}

shiva_jolad at 2007-6-29 0:32:57 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 5

The two main benefits here (albeit minimal) are the functionality inherited from EventObject and the fact that it makes your unique event type standard and thus interchangeable with other listeners and objects in the Swing/AWT API. Often, implementers of event types won't care about the main function ( getSource() ) offered by EventObject but will extend anyway to get the latter mentioned benefit. It makes your object ten times more reusable someday, later on, even when you can't envision an immediate need.

boothben at 2007-6-29 0:32:57 > top of Java-index,Archived Forums,New To Java Technology Archive...