0 Replies Latest reply: Apr 15, 2013 3:03 AM by 1003036 RSS

    JavaFX Dragboard setContent interfering with setOnMouseDragged?

    1003036
      I would like to use drag and drop in JavaFX and have a feature where you can visually see the item being dragged. I've implemented what I thought would be a working solution, but there seems to be an issue with the API.

      According to the API startDragAndDrop(TransferMode...) MUST be initialized inside setOnDragDetected. This is where I use a Dragboard to store content I'd like to transfer to another node.

      EDIT: This appears to have something to do with the TrasferMode. If I use TransferMode.NONE there is no issue, but use of COPY, ANY, LINK always results in this problem.

      But calling dragBoard.setContent(some clipboard content) only allows very small increments of movement with the mouse (a max of 4 pixels in any direction!). Removing this line of code, I can then drag the item and see it being dragged anywhere, but of course, I then can't store clipboard content.

      The problem I see is that setOnMouseDragged(..) gets called before setOnDragDetected! It doesn't make much sense why setOnMouseDragged gets run before setOnDragDetected...

      Is there something obvious to you in my code that's maybe causing a problem? I'd simply like to be able to see the imgView moving when dragging and be able to drop it on a target as usual, with the clipboard content.


      private ImageView initImageView(Image img){

      /** Used to keep track of the position in createImageView**/
      final DoubleProperty initX = new SimpleDoubleProperty();
      final DoubleProperty initY = new SimpleDoubleProperty();
      final ObjectProperty<Point2D> dragAnchor = new SimpleObjectProperty<>();
      final ImageView imgView = new ImageView(img);
      final DoubleProperty newXPosition = new SimpleDoubleProperty();
      final DoubleProperty newYPosition = new SimpleDoubleProperty();
      final DoubleProperty dragX = new SimpleDoubleProperty();
      final DoubleProperty dragY = new SimpleDoubleProperty();

      imgView.setOnDragDetected(new EventHandler<MouseEvent>() {
      public void handle(MouseEvent event) {
      System.out.println("DRAG DETECTED");
      ///imgView.setVisible(false);
      ClipboardContent content = new ClipboardContent();
      content.putString(token.getImage());

      Dragboard db = imgView.startDragAndDrop(TransferMode.MOVE);
      db.setContent(content); //makes full dragging impossible!!
      event.consume();
      }
      });

      imgView.setOnMousePressed(new EventHandler<MouseEvent>() {
      public void handle(MouseEvent event) {

      initX.set(imgView.getTranslateX());
      initY.set(imgView.getTranslateY());
      dragAnchor.set(new Point2D(event.getSceneX(), event.getSceneY()));
      event.consume();
      }
      });

      imgView.setOnMouseDragged(new EventHandler<MouseEvent>() {
      public void handle(MouseEvent me) {

      System.out.println("DRAGGING");

      dragX.set(me.getSceneX() - dragAnchor.get().getX());
      dragY.set(me.getSceneY() - dragAnchor.get().getY());
      imgView.setOpacity(0.5);
      newXPosition.set(initX.get() + dragX.get());
      newYPosition.set(initY.get() + dragY.get());

      imgView.setTranslateX(newXPosition.get());
      imgView.setTranslateY(newYPosition.get());
      me.consume();
      }
      });

      imgView.setOnDragDone(new EventHandler<DragEvent>() {
      public void handle(DragEvent event) {

      if (event.getTransferMode() == TransferMode.MOVE) {
      System.out.println("RHS TOKEN::: setOnDragDone called: " + token);
      System.out.println("This clears the sources data");

      //imgView.setVisible(false); //TEST ONLY
      }
      event.consume();
      }
      });


      imgView.setOnMouseEntered(new EventHandler <MouseEvent>() {
      public void handle(MouseEvent event) {
      imgView.setEffect(new Glow(0.5));
      event.consume();
      }
      });

      imgView.setOnMouseExited(new EventHandler <MouseEvent>() {
      public void handle(MouseEvent event) {
      imgView.setEffect(new Glow(0.0));
      imgView.setOpacity(1);
      event.consume();
      }
      });

      return imgView;
      }