2012-11-29 76 views
7

我在JavaFx 2.2中構建了一個應用程序,它包含一個分隔面板,其左側是組件面板,右側是工作表。基本上我想要做的是一個簡單的wysiwyg編輯器,您可以將組件從左側拖到右側,然後將它們安排在右側。在場景生成器中實現拖放操作

我花了幾天試圖實現具有SceneBuilder相同的拖放和拖放功能,沒有運氣..

http://docs.oracle.com/javafx/2/drag_drop/HelloDragAndDrop.java.html樣我已經得到了一個拖拽拖放工作,但我找不到任何方法來更改拖動時出現的默認文件圖標(並將其替換爲我正在拖動的組件的快照)以及當您結束某些操作時顯示禁止圖標你不能放棄。

任何幫助(可能是諮詢,代碼片段,樣品或其他人),將不勝感激:)

謝謝!

回答

16

[更新]

最後管理它自己:

/* The 'sceneRoot' object is the root Node of the scene graph 
* stage.setScene(new Scene(sceneRoot, 1280, 1024)); 
*/ 

private ImageView dragImageView = new ImageView(); 
private Node dragItem; 

_

rightPane.setOnMouseDragEntered(new EventHandler<MouseDragEvent>() { 
    public void handle(MouseDragEvent e) { 
     rightPane.setStyle("-fx-border-color:red;-fx-border-width:2;-fx-border-style:solid;"); 
     e.consume(); 
    } 
}); 
rightPane.setOnMouseDragExited(new EventHandler<MouseDragEvent>() { 
    public void handle(MouseDragEvent e) { 
     rightPane.setStyle("-fx-border-style:none;"); 
     e.consume(); 
    } 
}); 
rightPane.setOnMouseDragReleased(new EventHandler<MouseDragEvent>() { 
    public void handle(MouseDragEvent e) { 
     //TODO: add new instance of dragItem to rightPane 
     e.consume(); 
    } 
}); 

_

private void addGesture(final Node node) { 
    node.setOnDragDetected(new EventHandler<MouseEvent>() { 
     public void handle(MouseEvent e) { 

      SnapshotParameters snapParams = new SnapshotParameters(); 
      snapParams.setFill(Color.TRANSPARENT); 
      dragImageView.setImage(node.snapshot(snapParams, null)); 

      sceneRoot.getChildren().add(dragImageView); 

      dragImageView.startFullDrag(); 
      e.consume(); 
     } 
    }); 
    node.setOnMouseDragged(new EventHandler<MouseEvent>() { 
     public void handle(MouseEvent e) { 
      Point2D localPoint = sceneRoot.sceneToLocal(new Point2D(e.getSceneX(), e.getSceneY())); 
      dragImageView.relocate(
        (int)(localPoint.getX() - dragImageView.getBoundsInLocal().getWidth()/2), 
        (int)(localPoint.getY() - dragImageView.getBoundsInLocal().getHeight()/2) 
      ); 
      e.consume(); 
     } 
    }); 
    node.setOnMouseEntered(new EventHandler<MouseEvent>() { 
     public void handle(MouseEvent e) { 
      node.setCursor(Cursor.HAND); 
     } 
    }); 
    node.setOnMousePressed(new EventHandler<MouseEvent>() { 
     public void handle(MouseEvent e) { 
      dragItem = node; 
      dragImageView.setMouseTransparent(true); 
      node.setMouseTransparent(true); 
      node.setCursor(Cursor.CLOSED_HAND); 
     } 
    }); 
    node.setOnMouseReleased(new EventHandler<MouseEvent>() { 
     public void handle(MouseEvent e) { 
      dragItem = null; 
      dragImageView.setMouseTransparent(false); 
      node.setMouseTransparent(false); 
      node.setCursor(Cursor.DEFAULT); 
      sceneRoot.getChildren().remove(dragImageView); 
     } 
    }); 

} 
+1

好的工作。計劃在未來版本中添加[drag view feature](http://javafx-jira.kenai.com/browse/RT-14730?focusedCommentId=317064#)到JavaFX平臺,可能是jdk8。 – jewelsea

+0

我嘗試重新實現ListView的解決方案,但不知道setOnDragDetected處理程序中的場景變量是什麼類型。它不是Scene,因爲Scene沒有getChildern()方法。 – andreas

+0

'場景'對象實際上是場景圖的根節點。我已經更新了我的代碼,並通過sceneRoot更名爲了更好的理解。感謝您突出顯示;) – Badisi

2

也許晚了,但與setDragView選項,現在是更多simpple :)

// Cursor Display for Drag&Drop 
source.setOnMouseEntered(e -> source.setCursor(Cursor.OPEN_HAND)); 
source.setOnMousePressed(e -> source.setCursor(Cursor.CLOSED_HAND)); 
source.setOnMouseReleased(e -> source.setCursor(Cursor.DEFAULT)); 

// Manage drag 
source.setOnDragDetected(event -> { 
    /* drag was detected, start a drag-and-drop gesture*/ 
    Dragboard db = source.startDragAndDrop(TransferMode.MOVE); 

    // Visual during drag 
    SnapshotParameters snapshotParameters = new SnapshotParameters(); 
    snapshotParameters.setFill(Color.TRANSPARENT); 
    db.setDragView(source.snapshot(snapshotParameters, null)); 

    /* Put a string on a dragboard */ 
    ClipboardContent content = new ClipboardContent(); 
    content.putString(source.getText()); 
    db.setContent(content); 

    event.consume(); 
});