function as parameter?

W

wEEdpEckEr

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
 
S

Sudsy

wEEdpEckEr said:
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.
 
J

Joona I Palaste

wEEdpEckEr said:
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.
 
N

Neil Campbell

wEEdpEckEr said:
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.
 
W

wEEdpEckEr

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
 
W

wEEdpEckEr

Sudsy <[email protected]> schreef op zo, 04 jan 2004
22:11:41 GMT
in
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
 
S

Sudsy

wEEdpEckEr said:
Joona I Palaste <[email protected]> schreef op zo, 04 jan 2004
22:11:57 GMT in



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

;-)
 
W

wEEdpEckEr

- 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/w3c/jwput/gui/StringDialog.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
 
S

Sudsy

wEEdpEckEr said:
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/w3c/jwput/gui/StringDialog.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!
 
A

Andrew Thompson

| Sudsy <[email protected]> schreef op ma, 05 jan 2004
00:26:21 GMT
| in ...
| ..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/w3c/jwput/gui/StringDialog.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");
 
W

wEEdpEckEr

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

this one does the trick indeed... I think I'll stick to this, since I'm
using swing for the gui anyways. It's just weird that I never stumbled on
to this one searching google for "java dialog returns string". Thanx for
you help!

greetz
Tim
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top