dimanche 25 février 2018

Java Where should i create eventHandlers?

Lets imagine i have relative big program, where should i write the eventHandlers bodys?

Imagine the following scenario:

public class MyClass extends OtherClass{

    private int a;
    private Object x;

    public MyClass(){
        super();

        //Adding EventHandlers
        addEventFilter( MouseEvent.MOUSE_PRESSED, getOnMousePressedEventHandler());
        addEventFilter( MouseEvent.MOUSE_RELEASED, getOnMouseDraggedEventHandler());
    }

    //Event Handler Body
    EventHandler<MouseEvent> getOnMousePressedEventHandler() {
        return onMousePressedEventHandler;
    }
    EventHandler<MouseEvent> getOnMouseDraggedEventHandler() {
        return onMouseDraggedEventHandler;
    }
    private EventHandler<MouseEvent> onMousePressedEventHandler = event -> {
        //Do something
    };
    private EventHandler<MouseEvent> onMouseDraggedEventHandler = event -> {};
}

I was thinking to bring the Event Handler Body to other class or a abstract class, however there is a few variables like x that are needed on the event handlers. So is there a correct way for doing what i want? I have a few options but all of them sound terrible and create a class like private MyClassEventHandler handler and do MyClassEventHandler = new MyClassEventHandler() on the construtor looks very bad.

Aucun commentaire:

Enregistrer un commentaire