Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java replacing method with lambda

I have the following method

private void initializeMoveOnClick(final Group window){
    window.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent mouseEvent) {
            //do some stuff
        }
    });
}

How do I go about replacing the overrided handle method declaration with a lambda expression?

like image 949
Sahar Rabinoviz Avatar asked Feb 13 '23 10:02

Sahar Rabinoviz


1 Answers

You have mostly two ways to do it:

private void initializeMoveOnClick(final Group window){
    window.addEventFilter(MouseEvent.MOUSE_CLICKED, e -> runSomeMethod());
}

and:

private void initializeMoveOnClick(final Group window){
    window.addEventFilter(MouseEvent.MOUSE_CLICKED, this::eventFilter);
}
private void eventFilter(MouseEvent e) {
    //do some stuff
}
like image 88
assylias Avatar answered Feb 16 '23 02:02

assylias