Reputation: 51
How do I code a function like this one from lambda:
jButton.addActionListener(ae -> callAnyMethod());
because I am creating a library and would like to implement such a pattern on my own. Before Java 8 and lambda was released how would someone made such a pattern ? like what I am trying to approach is following:
I am trying to set a placeholder into the actionPerformed method of my CustomButton ActionListener and call a method like followed :
CustomButton.CustomButtonListener(placeholder method (); )
and the user needs just to create a method and write it inside the bricks ... For example the method named def() :
CustomButton.CustomButtonListener(def());
and def will be passed automatically to the actionPerformed method of my CustomButtonListener and will be fired on button click
Edit:
well that is the code I've came up with so far:
the ActionListener which is stored in my CustomButton class as a method:
public void CustomButtonListener(Object object){
addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// how to call the method stored in the Object "object" here? and actually run it?
}
});
and the code snippet from the button:
CustomButton button = new CustomButton();
button.CustomButtonListener(def());
public void def(){
String a = "lambda!";
System.out.print("a");
}
Upvotes: 1
Views: 9779
Reputation: 125
If I did not misunderstood your question, your syntax is already correct.
public static void main() {
...
jButton.addActionListener(e -> myListener());
...
}
...
public void myListener(){
dosomething();
}
which is the shorthand for this:
public static void main() {
...
MyListener myListener = new MyListener();
jButton.addActionListener(myListener);
...
}
public class MyListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
dosomething();
}
}
For more information, you can refer to this.
For more detailed explantion you can also refer to this, under the Lambda Type Inference section.
Upvotes: 3
Reputation: 25903
Lambda expressions were introduced in Java 8. If you're using an earlier Java version, you can implement the same pattern using classes and interfaces. Just create a class that implements the ActionListener
interface and the corresponding method. You can make this shorter by using anonymous classes. You then create an instance of this class and pass it to the addActionListener
method. More or less, lambdas do exactly the same (however, they may have an improved performance).
Here is an example:
public static void main() {
...
MyListener myListener = new MyListener();
jButton.addActionListener(myListener);
...
}
public class MyListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Triggered!");
}
}
And with anonymous classes:
public static void main() {
...
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Triggered!");
}
});
...
}
Upvotes: 2
Reputation: 6473
You have to identify which Interface
your listener uses and declare an anonymous Class.
Lets say that jButton.addActionListener(...)
waits for a ActionListener
:
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
callAnyMethod();
}
});
Lambdas, which were implemented in Java 8 natively, are another way to access methods and Functional Interfaces
in an easy and readable way. The compiler detects automatically what it should use and calls it.
I do not know your project, but if you can afford it, learning how to use Lambdas (and Streams) will greatly increase your productivity, make your code simpler, more readable, and less bug-prone.
Upvotes: 1