Search code examples
javaeventsjavafxmouseevent

MouseEvent.Moved, detection stops working when mouse button is being pressed


I have a program that tracks the mouse with MouseEvent.MOVED in javafx and whenever i press and hold the mouse button the tracking stops.

I have tried to switch events from addEventFilter to addEventHandler. Adding another Event, MouseEvent.DRAGED. But it wont even register an event until i disable the code from MouseEvent.Moved. I have tried to combine these but nothing seems to work. Help is very much appreciated.

EventHandler<MouseEvent> tracking = new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent e){
            double x = e.getSceneX();
            double y = e.getSceneY();

            if((x + size < 400) && (y - circle.getRadius() > 1)){
                switch (value){
                    case 0 :
                        circle.setCenterX(x);
                        circle.setCenterY(y);
                        break;
                    case 1 :
                        rec.setLayoutX(x);
                        rec.setLayoutY(y);
                        break;
                    case 2 :
                        pol.getPoints().clear();
                        pol.getPoints().addAll(new Double[]{x - size, y,      x + size, y, x, y + size});
                        break;
                }
            }
        }
    }; 
    EventHandler<MouseEvent> test = new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent e){
           System.out.print("test: ");
        }
    }; 


pane1.addEventHandler(MouseEvent.MOUSE_MOVED, tracking);
pane1.addEventFilter(MouseEvent.MOUSE_DRAGGED, test);

Solution

  • Okey i found why it diden´t work. The MouseEvent was actually being activated on the object that was tracking the mouse. So when i clicked the mouse it created a drag event on that object that woulden´t end until i released the mouse button, thanks for the help :)

    edit: I still dont get why it dident work from the beginning. Shouldent the eventHandler method capture the event when it bubbles up?