Results 1 to 3 of 3
  1. #1
    martypapa is offline Member
    Join Date
    Feb 2010
    Posts
    21
    Rep Power
    0

    Default Function as argument?

    Is it possible to send a method as an argument to another object?

    I have several buttons that I made myself, that extend the JComponent Class. I don't want them all to perform the same method when they are clicked, so how do I achieve this?

    I've scanned the net and found abstractActions, but I'm not sure if this is the way to go.

    Here is an example of what I would like to achieve:

    Java Code:
    public class MyClass{
    [INDENT]
    public MyClass(){
    [INDENT]
    MyComponent button1 = new MyComponent();
    MyComponent button2 = new MyComponent();
    
    
    //I know this part is wrong, so how can I do it?
    button1.clickMethod = goToMenu();
    
    button2.clickMethod = doSomethingElse();
    [/INDENT]
    }[/INDENT]
    
    
    }
    
    public class MyComponent extends JComponenet{
    ?Function? clickMethod();
    ...
    [INDENT]//Code for click
    public void onClick(){
    //do clickMethod()
    }[/INDENT]
    
    }
    Thank you

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Define and use interfaces: e.g.

    Java Code:
    public interface Func1 { void func1(); }
    public interface Func2 { void func2(); }
    ...
    public interface Funcn { void funcn(); }
    ... and make your class implement one or more of those interfaces.

    kind regards,

    Jos

  3. #3
    martypapa is offline Member
    Join Date
    Feb 2010
    Posts
    21
    Rep Power
    0

Similar Threads

  1. Command line argument
    By denisatandi in forum New To Java
    Replies: 8
    Last Post: 10-16-2012, 11:37 PM
  2. Replies: 3
    Last Post: 12-04-2009, 03:08 PM
  3. Method as an argument?
    By StokedOnMe in forum New To Java
    Replies: 13
    Last Post: 09-18-2009, 06:29 AM
  4. Command Line Argument
    By right2001 in forum New To Java
    Replies: 6
    Last Post: 02-17-2009, 02:08 PM
  5. Parsing Argument Values
    By vipvan2000 in forum Advanced Java
    Replies: 1
    Last Post: 02-17-2008, 01:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •