拖动BorderPane正文

时间:2015-03-01 18:24:45

标签: javafx javafx-2 javafx-8

我使用此JavaFX代码将BorderPane拖到FlowPane中:

private Node dragPanel(Node bp)
    {
        bp.setOnDragDetected(new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent event)
            {
                Dragboard db = bp.startDragAndDrop(TransferMode.MOVE);
                ClipboardContent clipboard = new ClipboardContent();
                final int nodeIndex = bp.getParent().getChildrenUnmodifiable()
                    .indexOf(bp);
                clipboard.putString(Integer.toString(nodeIndex));
                db.setContent(clipboard);

                Image img = bp.snapshot(null, null);
                db.setDragView(img, 7, 7);

                event.consume();
            }
        });
        bp.setOnDragOver(new EventHandler<DragEvent>()
        {
            @Override
            public void handle(DragEvent event)
            {
                boolean accept = true;
                final Dragboard dragboard = event.getDragboard();
                if (dragboard.hasString())
                {
                    try
                    {
                        int incomingIndex = Integer.parseInt(dragboard.getString());
                        int myIndex = bp.getParent().getChildrenUnmodifiable()
                            .indexOf(bp);
                        if (incomingIndex == myIndex)
                        {
                            accept = false;
                        }
                    }
                    catch (java.lang.NumberFormatException e)
                    {
                        // handle null or not number string in clipboard
                        accept = false;
                    }
                }
                else
                {
                    accept = false;
                }
                if (accept)
                {
                    event.acceptTransferModes(TransferMode.MOVE);
                }
            }
        });
        bp.setOnDragDropped(new EventHandler<DragEvent>()
        {
            @Override
            public void handle(DragEvent event)
            {
                boolean success = false;
                final Dragboard dragboard = event.getDragboard();
                if (dragboard.hasString())
                {
                    try
                    {
                        int incomingIndex = Integer.parseInt(dragboard.getString());
                        final Pane parent = (Pane) bp.getParent();
                        final ObservableList<Node> children = parent.getChildren();
                        int myIndex = children.indexOf(bp);
                        final int laterIndex = Math.max(incomingIndex, myIndex);
                        Node removedLater = children.remove(laterIndex);
                        final int earlierIndex = Math.min(incomingIndex, myIndex);
                        Node removedEarlier = children.remove(earlierIndex);
                        children.add(earlierIndex, removedLater);
                        children.add(laterIndex, removedEarlier);
                        success = true;
                    }
                    catch (java.lang.NumberFormatException e)
                    {
                        //TO DO... handle null or not number string in clipboard
                    }
                }
                event.setDropCompleted(success);
            }
        });
//        bp.setMinSize(50, 50);
        return bp;
    }

我使用以下代码启用此拖动事件:

BorderPane panel = new BorderPane();
dragPanel(panel),

我也调整了代码,它也被激活了。我需要一些方法来应用我只需单击并拖动面板的拖动代码。我想在拖动面板边框时禁用拖动侦听器。有没有办法限制这个?

1 个答案:

答案 0 :(得分:1)

我猜“边界”你只是指边框窗格的边缘。您只需检查鼠标事件的坐标,只有在离开边界时才启动拖动。为此,您需要知道边框窗格的宽度和高度。获取这些内容的方法在Region中定义,因此您需要将参数类型从Node缩小到Region。如果您拨打dragPanel(panel),但仍无法传递不属于Node个实例的Region,这仍然有用。

final int borderSize = 5 ;

// ...
private Node dragPane(Region bp) {

    bp.setOnDragDetected(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            double x = event.getX();
            double y = event.getY();
            double width = bp.getWidth();
            double height = bp.getHeight();
            if (x > borderSize && x < width - borderSize 
                  && y > borderSize && y < height - borderSize) {

                Dragboard db = bp.startDragAndDrop(TransferMode.MOVE);
                ClipboardContent clipboard = new ClipboardContent();
                final int nodeIndex = bp.getParent().getChildrenUnmodifiable()
                    .indexOf(bp);
                clipboard.putString(Integer.toString(nodeIndex));
                db.setContent(clipboard);

                Image img = bp.snapshot(null, null);
                db.setDragView(img, 7, 7);

                event.consume();
            }
        }
    });

    // ...
}