function as parameter?

Discussion in 'Java' started by wEEdpEckEr, Jan 4, 2004.

  1. wEEdpEckEr

    wEEdpEckEr Guest

    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
     
    wEEdpEckEr, Jan 4, 2004
    #1
    1. Advertising

  2. wEEdpEckEr

    Sudsy Guest

    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.
     
    Sudsy, Jan 4, 2004
    #2
    1. Advertising

  3. 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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Products like that make me wish I could menstruate."
    - Andy Richter
     
    Joona I Palaste, Jan 4, 2004
    #3
  4. 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
     
    Neil Campbell, Jan 4, 2004
    #4
  5. wEEdpEckEr

    wEEdpEckEr Guest

    Joona I Palaste <> schreef op zo, 04 jan 2004
    22:11:57 GMT in news:bta33d$7o7$:

    > 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
     
    wEEdpEckEr, Jan 5, 2004
    #5
  6. wEEdpEckEr

    wEEdpEckEr Guest

    Sudsy <> schreef op zo, 04 jan 2004
    22:11:41 GMT
    in news::

    > 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
     
    wEEdpEckEr, Jan 5, 2004
    #6
  7. wEEdpEckEr

    Sudsy Guest

    wEEdpEckEr wrote:
    > Joona I Palaste <> schreef op zo, 04 jan 2004
    > 22:11:57 GMT in news:bta33d$7o7$:
    >
    >
    >>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

    ;-)
     
    Sudsy, Jan 5, 2004
    #7
  8. wEEdpEckEr

    wEEdpEckEr Guest

    Sudsy <> schreef op ma, 05 jan 2004 00:26:21 GMT
    in news::

    > - 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
     
    wEEdpEckEr, Jan 5, 2004
    #8
  9. wEEdpEckEr

    Sudsy Guest

    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/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!
     
    Sudsy, Jan 5, 2004
    #9
  10. "wEEdpEckEr" <toch_lekker_nie@geen_email.aub> wrote in message
    news:Xns9467172FFB92Aweedpeckerwanadoobe@195.130.132.68...
    | Sudsy <> schreef op ma, 05 jan 2004
    00:26:21 GMT
    | in news::
    ...
    | ..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");

    --
    Andrew Thompson
    * http://www.PhySci.org/ PhySci software suite
    * http://www.1point1C.org/ 1.1C - Superluminal!
    * http://www.AThompson.info/andrew/ personal site
     
    Andrew Thompson, Jan 5, 2004
    #10
  11. Andrew Thompson, Jan 5, 2004
    #11
  12. wEEdpEckEr

    wEEdpEckEr Guest

    "Andrew Thompson" <> schreef op ma, 05 jan 2004
    08:06:57 GMT in news:BW8Kb.78277$:

    > 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
     
    wEEdpEckEr, Jan 5, 2004
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    523
    Matteo
    Mar 29, 2007
  2. Stuart Redmann
    Replies:
    5
    Views:
    483
    Stuart Redmann
    Dec 14, 2007
  3. Replies:
    5
    Views:
    644
  4. Mister B
    Replies:
    8
    Views:
    557
    Nick Keighley
    Aug 26, 2010
  5. AzamSharp
    Replies:
    2
    Views:
    181
Loading...

Share This Page