Hello. I have a program that calls a method from another class. This
method takes a few seconds to execute as it uses a timer. I want a way
of telling the caller when the callee is finished. My idea was passing
a parameter to the callee containing the name of the method to run on
completion. This method would be in the caller's class.
So basically, how do i pass a method name to another class so that it
can be called at a later time?
Or, what better ways are there to be notified after a timer using
method has completed? (other than another timer as the timed code can
be finished by clicking the screen)
thanks!
Eric Sosman - 01 Mar 2007 20:13 GMT
stinkinrich88@googlemail.com wrote On 03/01/07 14:41,:
> Hello. I have a program that calls a method from another class. This
> method takes a few seconds to execute as it uses a timer. I want a way
[quoted text clipped - 8 lines]
> method has completed? (other than another timer as the timed code can
> be finished by clicking the screen)
It's not quite clear to me what you're doing. The normal
way for a callee to notify a caller that it's finished is for
the callee to return ...
If you're looking for something like a "callback" or a
"strategy pattern," the sort of thing C uses function pointers
for, that's easy: You pass the callee an object that has the
method you'd like the callee to "call back" to. For example,
consider how a Comparator is used; there's nothing magical
about a Comparator, and you can do similar things in your
own classes.

Signature
Eric.Sosman@sun.com
Dimitri Kurashvili - 01 Mar 2007 20:21 GMT
On Mar 1, 7:41 pm, "stinkinric...@googlemail.com"
<stinkinric...@googlemail.com> wrote:
> Hello. I have a program that calls a method from another class. This
> method takes a few seconds to execute as it uses a timer. I want a way
[quoted text clipped - 10 lines]
>
> thanks!
As i understand you need to run some code in a new Thread and then
notify another part when this code will be completed. If you wish that
a caller part not to be blocked while executing new thread, then the
better method is to use listeners; which will be notified by called
method when execution will be completed.
If you preffer for whatever reason to pass name of the method i think
you need also pass instance of caller object. Then it is simple to
call the method using java reflection api.
stinkinrich88@googlemail.com - 01 Mar 2007 22:10 GMT
hmm, say I had a method like this:
public void show(String messageIn)
{
Timer time = new Timer(2000, new ActionListener() {
public void actionPerformed(ActionEvent e){hide();}});
msg.setText(message);
time.setRepeats(false);
time.start();
setVisible(true);
}
public void hide()
{
setVisible(false);
***NOTIFY SOMEHOW****
}
This displays a message on screen. The message dissapears after 2
seconds, OR when the user clicks it (an actionListener is added to it
in the constructor). This means the hide method will not necisarily
run after 2 seconds. When it does run I want it to notify the class
that called it (a separate class) by either running a method from it
or having the class wait for it somehow
Dimitri Kurashvili - 02 Mar 2007 05:22 GMT
On Mar 1, 6:10 pm, "stinkinric...@googlemail.com"
<stinkinric...@googlemail.com> wrote:
> hmm, say I had a method like this:
>
[quoted text clipped - 21 lines]
> setVisible(false);
> ***NOTIFY SOMEHOW****
A. -------------------------
// add field theCaller and initialize it
// at appropriate time
theCaller.notifyMethodName(...);
B. -------------------------
fireEndOfExecutionEvent(new MyEvent(...));
|
|--> all listeners to be called
> }
>
[quoted text clipped - 4 lines]
> that called it (a separate class) by either running a method from it
> or having the class wait for it somehow
stinkinrich88@googlemail.com - 02 Mar 2007 11:12 GMT
> On Mar 1, 6:10 pm, "stinkinric...@googlemail.com"
>
[quoted text clipped - 43 lines]
> > that called it (a separate class) by either running a method from it
> > or having the class wait for it somehow
wow, that's over my head I'm afraid. Could you break it down a little
please? Are A&B two separate solutions? I tried to google parts of
them and I got nothing! Thanks for your help!!!
Gordon Beaton - 02 Mar 2007 11:30 GMT
> wow, that's over my head I'm afraid. Could you break it down a little
> please? Are A&B two separate solutions? I tried to google parts of
> them and I got nothing! Thanks for your help!!!
Define an interface with some method that you want to be able to call.
Create an object that implements that interface and pass it to your
long-running method. When the long-running method is done, it calls
the method defined by the interface on the object you passed to it.
http://www.javaworld.com/javaworld/javatips/jw-javatip10.html
Search for "java callback" for more examples of this common technique.
/gordon

Signature
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
stinkinrich88@googlemail.com - 02 Mar 2007 16:25 GMT
> > wow, that's over my head I'm afraid. Could you break it down a little
> > please? Are A&B two separate solutions? I tried to google parts of
[quoted text clipped - 14 lines]
> [ don't email me support questions or followups ]
> g o r d o n + n e w s @ b a l d e r 1 3 . s e
Ahhh! pure genius! thank you soo much. That took me ages to get my
head around but it works well!
I don't suppose there's any way to do this without needing an
interface?
ooh, can I just ask a really easy question:
if I am doing lots of stuff to one thing, eg:
hello.setVisible(true);
hello.setColor(black);
hello.shutUp();
hello.blahblahblah();
how can I get it more like:
with(hello)
{
setVisible(true)
setColor(black)
......etc
what's the format?
thanks!!!!!!!!!!!
vjg - 02 Mar 2007 22:08 GMT
On Mar 2, 10:25 am, "stinkinric...@googlemail.com"
<stinkinric...@googlemail.com> wrote:
> > > wow, that's over my head I'm afraid. Could you break it down a little
> > > please? Are A&B two separate solutions? I tried to google parts of
[quoted text clipped - 41 lines]
>
> thanks!!!!!!!!!!!- Hide quoted text -
There's no built-in support for that kind of syntax (unless a newer
version has something I don't know about.... but you can use a nasty
looking hack like this:
Have all your setXXX methods return "this" (the instance of the class
you're working with) and then you can use this syntax -
hello.setVisible().setColor().etc();
I don't like it... but you could do it.
- Virgil
stinkinrich88@googlemail.com - 02 Mar 2007 22:51 GMT
> On Mar 2, 10:25 am, "stinkinric...@googlemail.com"
>
[quoted text clipped - 58 lines]
>
> - Virgil
Really? not supported? mental. I'm just use to VB6. Thanks anyway
though!! and thanks for that other solution! very imaginative!