Results 1 to 3 of 3
Thread: Function as argument?
- 02-07-2010, 08:02 AM #1
Member
- Join Date
- Feb 2010
- Posts
- 21
- Rep Power
- 0
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:
Thank youJava 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] }
- 02-07-2010, 10:00 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Define and use interfaces: e.g.
... and make your class implement one or more of those interfaces.Java Code:public interface Func1 { void func1(); } public interface Func2 { void func2(); } ... public interface Funcn { void funcn(); }
kind regards,
Jos
- 02-07-2010, 10:02 AM #3
Member
- Join Date
- Feb 2010
- Posts
- 21
- Rep Power
- 0
Similar Threads
-
Command line argument
By denisatandi in forum New To JavaReplies: 8Last Post: 10-16-2012, 11:37 PM -
Possible? Callback function passed as arguments to another function
By TreyAU21 in forum Advanced JavaReplies: 3Last Post: 12-04-2009, 03:08 PM -
Method as an argument?
By StokedOnMe in forum New To JavaReplies: 13Last Post: 09-18-2009, 06:29 AM -
Command Line Argument
By right2001 in forum New To JavaReplies: 6Last Post: 02-17-2009, 02:08 PM -
Parsing Argument Values
By vipvan2000 in forum Advanced JavaReplies: 1Last Post: 02-17-2008, 01:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks