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

    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{
    public MyClass(){
    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();
    public class MyComponent extends JComponenet{
    ?Function? clickMethod();
    [INDENT]//Code for click
    public void onClick(){
    //do clickMethod()
    Thank you

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power


    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,


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

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, 04: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, 03:08 PM
  5. Parsing Argument Values
    By vipvan2000 in forum Advanced Java
    Replies: 1
    Last Post: 02-17-2008, 02: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