我想在JavaFX中使用拖放功能,并具有一个可以直观地看到所拖动项目的功能。我已经实现了我认为会有效的解决方案,但是API似乎存在问题。

根据API,startDragAndDrop(TransferMode ...)必须在setOnDragDetected内部初始化。在这里,我可以使用Dragboard存储要转移到另一个节点的内容。

编辑:这似乎与TrasferMode有关。如果我使用TransferMode.NONE没有问题,但是使用COPY,ANY,LINK总是会导致此问题。

但是调用dragBoard.setContent(某些剪贴板内容)仅允许使用鼠标进行很小的移动增量(在任何方向上最多4个像素!)。删除此行代码,然后我可以将其拖动到任何地方,但当然我不能存储剪贴板内容。

我看到的问题是setOnMouseDragged(..)在setOnDragDetected之前被调用!为什么在setOnDragDetected之前运行setOnMouseDragged是没有意义的...

您的代码中有什么明显的东西可能会引起问题吗?我只是希望能够看到imgView在拖动时移动,并能够像往常一样将其与剪贴板内容一起拖放到目标上。

编辑2:更新下面的代码以反映仅使用拖动事件,而不是拖动和鼠标事件。同时使用会引起问题。仍然存在的问题是我无法放下目标,因为使用setOnDragOver使得拖动的节点始终位于光标下方。

protected ImageView initImageView(Image img){

    final Pane ldzPane = GameBoard.getInstance().getLDZpane();

    final ObjectProperty<Point2D> dragAnchor = new SimpleObjectProperty<>();

    final ImageView imgView = new ImageView(img);

    final DoubleProperty initX = new SimpleDoubleProperty();
    final DoubleProperty initY = new SimpleDoubleProperty();

    final DoubleProperty dragX = new SimpleDoubleProperty();
    final DoubleProperty dragY = new SimpleDoubleProperty();

    final DoubleProperty newXPosition = new SimpleDoubleProperty();
    final DoubleProperty newYPosition = new SimpleDoubleProperty();

    final int buffer = 3;

    imgView.setOnDragDetected(new EventHandler<MouseEvent>() {
        public void handle(MouseEvent event) {
            initX.set(imgView.getTranslateX());
            initY.set(imgView.getTranslateY());
            dragAnchor.set(new Point2D(event.getSceneX(), event.getSceneY()));
            ClipboardContent content = new ClipboardContent();
            content.putString(RHSIconizedToken.this.tokenLookupInfo());
            Dragboard db = imgView.startDragAndDrop(TransferMode.ANY);
            db.setContent(content);
            event.consume();
        }
    });

    imgView.setOnDragOver(new EventHandler<DragEvent>(){

        @Override
        public void handle(DragEvent event) {
            imgView.toFront();

            dragX.set(event.getSceneX() - dragAnchor.get().getX());
            dragY.set(event.getSceneY() - dragAnchor.get().getY());
            imgView.setOpacity(0.5);

            newXPosition.set(initX.get() + dragX.get());
            newYPosition.set(initY.get() + dragY.get());

            //if(       (Math.abs((newXPosition.get() - ldzPane.getWidth())) <=  ldzPane.getWidth() + startX + buffer) &&
            //      ((newXPosition.get() + startX + imgView.getImage().getWidth()+ buffer)<=  ldzPane.getWidth()))
                imgView.setTranslateX(newXPosition.get());

            //if(       (Math.abs((newYPosition.get() - ldzPane.getHeight())) <=  ldzPane.getHeight() + startY + buffer) &&
            //      ((newYPosition.get() + startY + imgView.getImage().getHeight()+ buffer)<=  ldzPane.getHeight()))
                imgView.setTranslateY(newYPosition.get());

            event.consume();
        }

    });

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

            LinkedList<RHSIconizedToken> iTokens = GameBoard.getInstance().getTokenBayItokens();
            if (event.getTransferMode() == TransferMode.MOVE) {
                RHSIconizedToken element = iTokens.remove(index);
                iTokens.add(index, new RHSIconizedToken(element.getImage(), new SourceToken("removed", "removed"), index));
                imgView.setVisible(false);

                GameBoard theGameBoard = GameBoard.getInstance();
                GUI theGUI = GUI.getInstance();
                //was this the last one removed from the rhs?
                //if so we need to signal the CompileButton to be turned on!
                if(theGameBoard.isRHSempty())
                    theGUI.enableCompileButton();
                else
                    theGUI.disableCompileButton();
            }
            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;
}

最佳答案

Java 8计划附带更多对视觉拖放的支持。 DragBoard的setDragView(Image,XOffset,YOffset)方法效果很好,并且在Java 8 beta中可用。

    imgView.setOnDragDetected(new EventHandler<MouseEvent>() {
        public void handle(MouseEvent event) {
            ClipboardContent content = new ClipboardContent();
            content.putString(RHSIconizedToken.this.tokenLookupInfo());
            Dragboard db = imgView.startDragAndDrop(TransferMode.ANY);
            db.setDragView(image, 7, 7);
            db.setContent(content);
            event.consume();
        }
    });

10-06 15:56