Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » OT: How to fire an action and listen in another class
OT: How to fire an action and listen in another class [message #453921] Wed, 13 April 2005 22:50 Go to next message
MP Mising name is currently offline MP Mising nameFriend
Messages: 18
Registered: July 2009
Junior Member
I have the need to create a class which is instantiated in another
class, does some work, and then needs to notify the parent class that it
is done (as well as return some data). I'm not sure how to create an
action method and make sure it is fired when the work is done, thus
notifying the parent class.

Anyone done something like this before? Is there a better way or am I
on the right track?

MP
Re: How to fire an action and listen in another class [message #453924 is a reply to message #453921] Wed, 13 April 2005 23:24 Go to previous message
Emil Crumhorn is currently offline Emil CrumhornFriend
Messages: 169
Registered: July 2009
Senior Member
Not related to SWT, but, just create your own listener and have the worker
class notify that interface when it's done.

Here's an example (not tested, wrote from scratch in this message):

public interface WorkerDone {
public void workerFinished(Object data);
}

public class Creator implements WorkerDone {
public Creator() {
Worker worker = new Worker();
worker.addListener(this);
worker.run();
}

public void workerFinished(Object data) {
System.err.println("I'm done! with data: " + data);
}
}

public class Worker {
Vector listeners = new Vector();

public Worker() {}

public void addListener(WorkerListener listener) {
listeners.add(listener);
}

public void run() {
// do some stuff that takes time
// ....
// notify everyone that cares that we finished
Object resultData = null;
notifyListeners(resultData);
}

public void notifyListeners(Object data) {
for (int i = 0; i < listeners.size(); i++) {
((WorkerListener)listeners.get(i)).workerFinished(data);
}
}
}

"MP" <dev1@levelgroundmedia.com> wrote in message
news:d3k7nv$gf4$1@news.eclipse.org...
>I have the need to create a class which is instantiated in another class,
>does some work, and then needs to notify the parent class that it is done
>(as well as return some data). I'm not sure how to create an action method
>and make sure it is fired when the work is done, thus notifying the parent
>class.
>
> Anyone done something like this before? Is there a better way or am I on
> the right track?
>
> MP
Previous Topic:How to propagate event to parent?
Next Topic:How to Change color content of an image
Goto Forum:
  


Current Time: Fri Apr 26 18:31:45 GMT 2024

Powered by FUDForum. Page generated in 0.03228 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top