call method by name

S

stinkinrich88

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!
 
E

Eric Sosman

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)

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.
 
D

Dimitri Kurashvili

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!


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.
 
S

stinkinrich88

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
 
D

Dimitri Kurashvili

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****

A. -------------------------
// add field theCaller and initialize it
// at appropriate time
theCaller.notifyMethodName(...);

B. -------------------------
fireEndOfExecutionEvent(new MyEvent(...));
|
|--> all listeners to be called
 
S

stinkinrich88

A. -------------------------
// add field theCaller and initialize it
// at appropriate time
theCaller.notifyMethodName(...);

B. -------------------------
fireEndOfExecutionEvent(new MyEvent(...));
|
|--> all listeners to be called

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!!!
 
G

Gordon Beaton

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
 
S

stinkinrich88

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


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!!!!!!!!!!!
 
V

vjg

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!!!!!!!!!!!- 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
 
S

stinkinrich88

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



Really? not supported? mental. I'm just use to VB6. Thanks anyway
though!! and thanks for that other solution! very imaginative!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top