Forum Stats

  • 3,854,538 Users
  • 2,264,379 Discussions
  • 7,905,721 Comments

Discussions

Getting the mouse drag release event on a canvas ?

3422412
3422412 Member Posts: 1
edited Apr 4, 2017 8:41AM in JavaFX 2.0 and Later

I have scene consisting of a single canvas. I'm using the canvas context to draw graphics on. I'm tracking the mouse location for input. Everything is working fine but I can't get the mouse drag release event to respond. I'm using the mouse press / release events to track button pushes. The standard press / release mouse events respond fine but after performing a drag the mouse drag release never happens.

Here is the function for my mouse events. It should print "drag release" but the mouse drag release event isn't happening.

private void startMouseInput(){

      

        scene.addEventFilter(MouseEvent.MOUSE_MOVED, new EventHandler<MouseEvent>() {

                   @Override

                   public void handle(MouseEvent event) {

                   mouse.lastLocation.set( event.getX() , event.getSceneY() );

                   event.consume();

                   }

         });

        scene.addEventFilter(MouseEvent.DRAG_DETECTED , new EventHandler<MouseEvent>() {

                   @Override

                   public void handle(MouseEvent event) {

        

                    scene.setCursor(Cursor.DEFAULT);

                    Dragboard db = scene.startDragAndDrop(TransferMode.ANY);

                    /* Put a string on a dragboard */

                    ClipboardContent content = new ClipboardContent();

                    content.putString("test");

                    db.setContent(content);

                    //mouse.lastLocation.set( event.getX() , event.getSceneY() );

                    scene.startFullDrag();

        

                    event.consume();

        

        

                   }

         });

        scene.addEventFilter(DragEvent.DRAG_OVER, new EventHandler<DragEvent>() {

                   @Override

                   public void handle(DragEvent event) {

                   mouse.lastLocation.set( event.getX() , event.getSceneY() );

                   event.consume();

                   }

        });

      

        scene.addEventFilter(MouseDragEvent.MOUSE_DRAG_RELEASED, new EventHandler<MouseDragEvent>() {

                   @Override

                   public void handle(MouseDragEvent event) {

                   System.out.println("drag release");

                   mouse.lastLocation.set( event.getX() , event.getSceneY() );

                   event.consume();

                   }

        });

      

        scene.addEventFilter(ScrollEvent.SCROLL, new EventHandler<ScrollEvent>() {

            @Override

            public void handle(ScrollEvent event) {

               for (MouseKey key : mouse.getKeys()) {

                         if (key.getTrigger() == MouseKeyTrigger.WHEEL){

                             ScrollEvent evt = (ScrollEvent)event;

                             key.setDelta(evt.getDeltaY());

                             mouse.fireMouseWheel( evt.getDeltaX() , evt.getDeltaY() );

                         }

               }

               event.consume();

            }

    });

       

         scene.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {

                   @Override

                   public void handle(MouseEvent event) {

                     for (MouseKey key : mouse.getKeys()){

                         if (event.getButton() ==  MouseButton.PRIMARY){

                             if (key.getTrigger() == MouseKeyTrigger.BUTTON1){

                                  key.triggered(true);

                             }

                         } else if (event.getButton() ==  MouseButton.SECONDARY){

                             if (key.getTrigger() == MouseKeyTrigger.BUTTON2){

                                  key.triggered(true);

                             }

                         } else if (event.getButton() ==  MouseButton.MIDDLE){

                             if (key.getTrigger() == MouseKeyTrigger.BUTTON3){

                                  key.triggered(true);

                             }

                         }

                     }

                     event.consume();

                   }

         });

       

         scene.addEventFilter(MouseEvent.MOUSE_RELEASED, new EventHandler<MouseEvent>() {

         @Override public void handle(MouseEvent event) {

                    for (MouseKey key : mouse.getKeys()){

                         if (event.getButton() ==  MouseButton.PRIMARY){

                             if (key.getTrigger() == MouseKeyTrigger.BUTTON1){

                                  key.triggered(false);

                             }

                         } else if (event.getButton() ==  MouseButton.SECONDARY){

                             if (key.getTrigger() == MouseKeyTrigger.BUTTON2){

                                  key.triggered(false);

                             }

                         } else if (event.getButton() ==  MouseButton.MIDDLE){

                             if (key.getTrigger() == MouseKeyTrigger.BUTTON3){

                                  key.triggered(false);

                             }

                         }

                         event.consume();

                  }

      }

    });

       

    }

Tagged:
jsmith

Answers

This discussion has been closed.