Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AbstractAction lambda

IntelliJ is suggesting that I change

new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            // my code here
        }
}

to

(AbstractAction) (e) -> {
    // my code here
}

When I change my code to the suggested code above, I get the message "target type of a lambda conversion must be an interface".


1 Answers

You should implement abstract action class, so you can use it functionally.

public class FunctionalAction extends AbstractAction {

    ActionListener myaction;

    public FunctionalAction(ActionListener customaction) {
        this.myaction = customaction;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        myaction.actionPerformed(e);
    }

    public ActionListener getMyaction() {
        return myaction;
    }

    public void setMyaction(ActionListener myaction) {
        this.myaction = myaction;
    }

}

You can use this class as below:

getTexfield().getActionMap().put(actionName, new FunctionalAction(ae -> {
        }));
like image 70
engtuncay Avatar answered May 18 '26 22:05

engtuncay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!