Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

My own Event

Status
Not open for further replies.

never

Programmer
Dec 2, 2001
18
GB
Hi all,
I try to find some eample with creating Java event but i found only with aplet and it is nothing what i need. so ;o)
I have some "class MyClass" and here is some "myMethod(Object o)". From time to time myMethod(Object o) is invoking and in this time when Object o income into myMethod I would like to invoke some MyEvent and on other side in some class will be listen myListener.PLEASE,can somebody to help me write "MyEvent" and "MyListener".

THX a lot

NeVeR
 
Try to have a Vector or ArrayList called listeners or something in the notifier class (the one that's to cast events). Then add a function like the following to it:

register(Listener listener)
{
listeners.add(listener);
}

With Listener being an interface that defines as follows:

interface Listener
{
void notify(MyEvent event);
}

Then the MyEvent class can be pretty basic, containing maybe a private caller variable and one that's for the message the caller wants to deliver. This class will maybe not be more than 20 lines long.

You then can implement a casting method in the notifier, like the following:

public void cast(MyEvent event)
{
for (int i = 0; i < listeners.size(); i++)
{
((Listener)listeners.get(i)).notify(event);
}
}

The notify function will then be the one listening for the actions.

You can also implement the Listener implementation in a class as inner class, making a more real action listener out of it :)

There is also an interface collection in the API, that defines pretty much the same thing. I don't remember the name though :-(

Hope this helps
allow thyself to be the spark that lights the fire
haslo@haslo.ch - www.haslo.ch​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top