2017-01-18 2 views
1

ドラッグアンドドロップ中にノードの半透明の「コピー」をマウスアイコンの隣に表示する最適な方法は何ですか?マウスアイコンの横にカスタムノードを持つJavaFXドラッグ&ドロップ

私は基本的に色付きの背景とテキストラベルを持つHBoxを持っています。マウスカーソルがドラッグされているときにマウスカーソルに「ついた」外観を与えたいと思います。

マウスカーソルがさまざまなドラッグアイコンに変化するのを見るのではなく、ユーザーがドラッグしているものを視覚的に確認できるのはいいことです。シーンビルダは、RadioButtonのようないくつかのコンポーネントをドラッグすると、これを行う傾向があります。

+2

あなたは(つまりあなたには、いくつかの時点でノード上の 'startDragAndDrop'を呼んでいる)「プラットフォームサポートドラッグアンドドロップ」やっていますか?もしそうなら、 'Dragboard.setDragView(...)'を呼び出すだけで、ドラッグのイメージカーソルを設定することができます。 –

+0

内部だけで、アプリケーションウィンドウの外には何もありません。 – User

+0

それは私が尋ねたものではありません。 –

答えて

2

ノードの「半透明な」コピーは、WritableImageを返すノード上でsnapshot(null, null)を呼び出して実行されます。次にDragBoardのドラッグビューにこのWritableImageを設定します。ここでこれを行う方法上の小さな例です。

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.Label; 
import javafx.scene.input.ClipboardContent; 
import javafx.scene.input.DataFormat; 
import javafx.scene.input.Dragboard; 
import javafx.scene.input.TransferMode; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 

public class DragAndDrop extends Application { 
    private static final DataFormat DRAGGABLE_HBOX_TYPE = new DataFormat("draggable-hbox"); 

    @Override 
    public void start(Stage stage) { 
     VBox content = new VBox(5); 

     for (int i = 0; i < 10; i++) { 
      Label label = new Label("Test drag"); 

      DraggableHBox box = new DraggableHBox(); 
      box.getChildren().add(label); 

      content.getChildren().add(box); 
     } 

     stage.setScene(new Scene(content)); 
     stage.show(); 
    } 

    class DraggableHBox extends HBox { 
     public DraggableHBox() { 
      this.setOnDragDetected(e -> { 
       Dragboard db = this.startDragAndDrop(TransferMode.MOVE); 

       // This is where the magic happens, you take a snapshot of the HBox. 
       db.setDragView(this.snapshot(null, null)); 

       // The DragView wont be displayed unless we set the content of the dragboard as well. 
       // Here you probably want to do more meaningful stuff than adding an empty String to the content. 
       ClipboardContent content = new ClipboardContent(); 
       content.put(DRAGGABLE_HBOX_TYPE, ""); 
       db.setContent(content); 

       e.consume(); 
      }); 
     } 
    } 

    public static void main(String[] args) { 
     launch(); 
    } 
} 
+0

私はRTFMで十分ではなかったので、スナップショットに(null、null)を渡すことができるかどうかわからなかった...とにかく、これはとにかく動作します。 – User

関連する問題