Java FX-在Label上移动drop and drop事件

时间:2016-05-13 21:27:28

标签: javafx javafx-2 javafx-8

我遇到了将标签拖放到彼此上的问题。我无法拖动标签并将其放到另一个标签上。检测到事件setOnDragDetected并且未检测到setOnDragDropped。这些是文件。

FXML:

                                                                                     
                                                                                                

           <HBox fx:id="hboxTwo" alignment="CENTER" spacing="100" style="-fx-background-color: orange;">
               <padding>
                   <javafx.geometry.Insets bottom="25" left="25" right="25" top="25" />    
               </padding>
              <children>
                  <Label fx:id="labelTwo" prefHeight="50.0" text="labelTWO" />
              </children>    
           </HBox>
       </children>

    </HBox>

</center>

控制器:

public class HomeController实现Initializable {

@FXML
Label labelOne;

@FXML
Label labelTwo;

@FXML
HBox hboxOne;

@FXML
HBox hboxTwo;

@Override
public void initialize(URL url, ResourceBundle rb) {
    labelOne.setOnDragDetected((MouseEvent event) -> {
        System.out.println("source::" + event.getSource());
    });

    hboxTwo.setOnDragDropped((DragEvent event) -> {
        event.acceptTransferModes(TransferMode.ANY);
        System.out.println("source::" + event.getSource());
    });

    hboxTwo.setOnDragEntered((DragEvent event) -> {
        event.acceptTransferModes(TransferMode.ANY);
        System.out.println("source::" + event.getSource());
    });
}

}

2 个答案:

答案 0 :(得分:1)

只需在下面复制并粘贴相关的拖拽检测代码,然后从JavaFX 8 Drag and Drop tutorial中删除。

将示例代码与代码进行比较时,您可以看到缺少的东西:

  1. 您没有构建包含拖放数据的Dragboard
  2. 您不是从Dropboard上的Dragboard中提取拖放数据。
  3. 您没有通知拖放事件已完成。
  4. 你没有适当地消费事件。
  5. source.setOnDragDetected(new EventHandler<MouseEvent>() {
        public void handle(MouseEvent event) {
            /* drag was detected, start a drag-and-drop gesture*/
            /* allow any transfer mode */
            Dragboard db = source.startDragAndDrop(TransferMode.ANY);
    
            /* Put a string on a dragboard */
            ClipboardContent content = new ClipboardContent();
            content.putString(source.getText());
            db.setContent(content);
    
            event.consume();
        }
    });
    
    target.setOnDragOver(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {
            /* data is dragged over the target */
            /* accept it only if it is not dragged from the same node 
             * and if it has a string data */
            if (event.getGestureSource() != target &&
                    event.getDragboard().hasString()) {
                /* allow for moving */
                event.acceptTransferModes(TransferMode.MOVE);
            }
    
            event.consume();
        }
    });
    
    target.setOnDragDropped(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {
            /* data dropped */
            /* if there is a string data on dragboard, read it and use it */
            Dragboard db = event.getDragboard();
            boolean success = false;
            if (db.hasString()) {
               target.setText(db.getString());
               success = true;
            }
            /* let the source know whether the string was successfully 
             * transferred and used */
            event.setDropCompleted(success);
    
            event.consume();
         }
    });
    

答案 1 :(得分:1)

要使setOnDragDropped有效,您必须先设置TransferMode,然后在setOnDragDropped中移除相应的方法调用:

 boxTwo.setOnDragOver((DragEvent event) -> {
        event.acceptTransferModes(TransferMode.ANY);
        event.consume();
 });

 boxTwo.setOnDragDropped((DragEvent event) -> {
        Label source = (Label) event.getGestureSource();
        boxTwo.getChildren().add(source);

        event.setDropCompleted()
        event.consume();
 });