Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > function as parameter?

Reply
Thread Tools

function as parameter?

 
 
wEEdpEckEr
Guest
Posts: n/a
 
      01-04-2004
Hi,

I was wondering if it is possible to pass on a function as parameter.
Because I have a class that extends a thread and uses a socket, and
listens constantly to this socket. Now when something passes over the
connection, the output needs to be displayed in a JTextArea. So what I
would like to do is:

public void displayOutput(String msg)
{
outputBox.append(msg);
}

and when initializing the thread:

server = new Server(this.displayOutput());

only I have no idea what I would have to in the constructor of the
Serverclass nor how to call the function from within the server class. I
know it's possible in C++, but about Java I'm not sure.

Thanx if anyone knows the answer to this.

greetings
Tim
 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      01-04-2004
wEEdpEckEr wrote:
> Hi,
>
> I was wondering if it is possible to pass on a function as parameter.
> Because I have a class that extends a thread and uses a socket, and
> listens constantly to this socket. Now when something passes over the
> connection, the output needs to be displayed in a JTextArea. So what I
> would like to do is:


You don't need a function pointer, the object reference is enough.

> public void displayOutput(String msg)
> {
> outputBox.append(msg);
> }
>
> and when initializing the thread:
>
> server = new Server(this.displayOutput());


server = new Server( this );

> only I have no idea what I would have to in the constructor of the
> Serverclass nor how to call the function from within the server class. I
> know it's possible in C++, but about Java I'm not sure.


public class Server {
ClassOrInterfaceWhichImplementsDisplayOutput myRef;
public Server( ClassOrInterfaceWhichImplementsDisplayOutput ref ) {
myRef = ref;
}
...
// somewhere in your code
myRef.displayOutput( someString );
}

Once you have an object reference then you can invoke methods on
the object.

 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      01-04-2004
wEEdpEckEr <toch_lekker_nie@geen_email.aub> scribbled the following:
> Hi,


> I was wondering if it is possible to pass on a function as parameter.
> Because I have a class that extends a thread and uses a socket, and
> listens constantly to this socket. Now when something passes over the
> connection, the output needs to be displayed in a JTextArea. So what I
> would like to do is:


> public void displayOutput(String msg)
> {
> outputBox.append(msg);
> }


> and when initializing the thread:


> server = new Server(this.displayOutput());


> only I have no idea what I would have to in the constructor of the
> Serverclass nor how to call the function from within the server class. I
> know it's possible in C++, but about Java I'm not sure.


> Thanx if anyone knows the answer to this.


You can't pass methods as arguments in Java. It would be possible to
add this functionality to Java, but it wouldn't be exactly trivial.
You can fake this by using Reflection, but that would be a hack. A
better way is to rethink your design.
If I were you, I would make an interface something like this:

public interface ServerCallback {
public void callback(String arg);
}

and then an implementation of it like this:

public class MainThread extends Thread implements ServerCallback {
/* the code in your thread class defined above goes here */
public void callback(String arg) {
displayOutput(arg);
}
}

and then just call this from your MainThread class:

server = new Server(this);

where the Server constructor expects a ServerCallback.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Products like that make me wish I could menstruate."
- Andy Richter
 
Reply With Quote
 
Neil Campbell
Guest
Posts: n/a
 
      01-04-2004
wEEdpEckEr wrote:

> Hi,
>
> I was wondering if it is possible to pass on a function as parameter.
> Because I have a class that extends a thread and uses a socket, and
> listens constantly to this socket. Now when something passes over the
> connection, the output needs to be displayed in a JTextArea. So what I
> would like to do is:
>
> public void displayOutput(String msg)
> {
> outputBox.append(msg);
> }
>
> and when initializing the thread:
>
> server = new Server(this.displayOutput());
>
> only I have no idea what I would have to in the constructor of the
> Serverclass nor how to call the function from within the server class. I
> know it's possible in C++, but about Java I'm not sure.
>
> Thanx if anyone knows the answer to this.
>
> greetings
> Tim


Please also see my reply in comp.lang.java.help.

--
Neil Campbell
batneil[AT]thebatcave[DOT]org[DOT]uk
http://www.thebatcave.org.uk
 
Reply With Quote
 
wEEdpEckEr
Guest
Posts: n/a
 
      01-05-2004
Joona I Palaste <(E-Mail Removed)> schreef op zo, 04 jan 2004
22:11:57 GMT in news:bta33d$7o7$(E-Mail Removed):

> and then just call this from your MainThread class:
>
> server = new Server(this);
>
> where the Server constructor expects a ServerCallback.


Whoosh, you're going way over my head, but since I've gotten a few other
possibilities, I will try them first. But thanx anyway for your fast reply!

greetz
Tim
 
Reply With Quote
 
wEEdpEckEr
Guest
Posts: n/a
 
      01-05-2004
Sudsy <(E-Mail Removed)> schreef op zo, 04 jan 2004
22:11:41 GMT
in news:(E-Mail Removed):

> You don't need a function pointer, the object reference is
> enough.


Alright, this looks possible. Together with Neil Campell's
solution I
will try both and see what suits best!

thanx for answering so fast!

greetz
<T!M> aka wEEdpEckEr
 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      01-05-2004
wEEdpEckEr wrote:
> Joona I Palaste <(E-Mail Removed)> schreef op zo, 04 jan 2004
> 22:11:57 GMT in news:bta33d$7o7$(E-Mail Removed):
>
>
>>and then just call this from your MainThread class:
>>
>>server = new Server(this);
>>
>>where the Server constructor expects a ServerCallback.

>
>
> Whoosh, you're going way over my head, but since I've gotten a few other
> possibilities, I will try them first. But thanx anyway for your fast reply!
>
> greetz
> Tim


Tim,
Joona is saying basically the same thing. He just went the interface
route to provide the method visibility. We're both suggesting that you
provide a reference to the constructor which is saved and can then be
used by methods in the instance to refer to the other object, the one
which implements the method you wish to use.
Which means either that:
- it's the standard approach and
- great minds think alike, or
- fools seldom differ



 
Reply With Quote
 
wEEdpEckEr
Guest
Posts: n/a
 
      01-05-2004
Sudsy <(E-Mail Removed)> schreef op ma, 05 jan 2004 00:26:21 GMT
in news:(E-Mail Removed):

> - it's the standard approach and
> - great minds think alike, or
> - fools seldom differ


if it's the standard approach I don't think you can be called fools,
right?

btw: since you're a great mind, I have another, probably stupid
question, but I can't google myself out of it. I have found that a
"StringDialog" exists (see:
http://jigsaw.w3.org/Winie/api/org/w...ingDialog.html )
but I have no idea how to get a string out of it, nor can I create such
a StringDialog (import javax.swing.Jpanel.*: won't do the trick). Well,
it doesn't have to be this particular dialogbox, I just want one that
has a little textfield on it in wich the user can fill in something and
from wich I can extract the String. Probably there's a pretty simple
solution, but as I said, google only refers me to the sort of pages as
mentioned above.

greetz and thanx in advance
Tim
 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      01-05-2004
wEEdpEckEr wrote:
> btw: since you're a great mind, I have another, probably stupid
> question, but I can't google myself out of it. I have found that a
> "StringDialog" exists (see:
> http://jigsaw.w3.org/Winie/api/org/w...ingDialog.html )
> but I have no idea how to get a string out of it, nor can I create such
> a StringDialog (import javax.swing.Jpanel.*: won't do the trick). Well,
> it doesn't have to be this particular dialogbox, I just want one that
> has a little textfield on it in wich the user can fill in something and
> from wich I can extract the String. Probably there's a pretty simple
> solution, but as I said, google only refers me to the sort of pages as
> mentioned above.


Tim,
That link timed-out on me but I can see that I wouldn't work by
including javax.swing.Jpanel as that package doesn't exist. Try
javax.swing.JPanel (notice the capitalization) and you might have
better luck.
In answer to the more general question, a modal dialog (as
opposed to a modeless one) will block the caller until it returns.
There is typically a method available which will provide the input
entered by the user (vis FileDialog).
So Java isn't infinitely flexible and can be a real stickler
when it comes to case but it's still a kick-ass language!

 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      01-05-2004
"wEEdpEckEr" <toch_lekker_nie@geen_email.aub> wrote in message
news:Xns9467172FFB92Aweedpeckerwanadoobe@195.130.1 32.68...
| Sudsy <(E-Mail Removed)> schreef op ma, 05 jan 2004
00:26:21 GMT
| in news:(E-Mail Removed):
...
| ..I have another, ....
| question, but I can't google myself out of it. I have found
that a
| "StringDialog" exists (see:
|
http://jigsaw.w3.org/Winie/api/org/w...ringDialog.htm
l )
| but I have no idea how to get a string out of it, nor can I
create such
| a StringDialog (import javax.swing.Jpanel.*: won't do the
trick).

try..
String inputValue = JOptionPane.showInputDialog(
"Please input a value");

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function versus pointer to function, in context of std::function,huh? Alf P. Steinbach C++ 10 07-27-2011 05:51 AM
Function pointer to void function and int function Giannis Papadopoulos C Programming 5 09-05-2005 09:06 PM
How override ALL function calls? (Is there a "function call function"?) seberino@spawar.navy.mil Python 2 08-01-2005 12:38 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Passing a C++ object's member function to a C function expecing a function pointer! James Vanns C++ 7 01-21-2004 02:39 AM



Advertisments