4

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?

0

2 Answers 2

5

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
}
Sign up to request clarification or add additional context in comments.

Comments

3

Since EventHandler is an interface with a single method, you can replace the anonymous class with lambda expression like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.