拖动后从源删除节点

时间:2015-08-26 00:21:05

标签: java javafx drag-and-drop label

我正在尝试创建一个程序,您可以将标签从屏幕的一侧拖到另一侧的框中,然后它们会在放入框中时移动。目前我可以拖放,但原始标签不会被删除。在创建标签之前,我必须访问文件并从中检索信息,然后将其解析为单独的标签,因此在访问文件之前都不会创建源标签或目标标签。如何在成功删除时删除源标签?

public void start(Stage primaryStage) {
// *** Styling and retrieving data for labels*** //

    //add blank spaces for answers
    for (int i=0;i<lenList.size();i++) {
        HBox ansBox = new HBox();
        ansBox.setAlignment(Pos.TOP_RIGHT);
        ansBox.setPadding(new Insets(5));
        ansBox.setSpacing(10);

        int length = Integer.parseInt(lenList.get(i));
        for (int l=0;l<length;l++) {
            Label rect = new Label();


            //make rectangle drag target
            rect.setOnDragOver(new EventHandler<DragEvent>() {
                @Override
                public void handle(DragEvent event) {
                    if (event.getGestureSource() != rect && event.getDragboard().hasString()) {
                        event.acceptTransferModes(TransferMode.MOVE);
                    }

                    event.consume();
                }
            });

            //what to do when syllable dropped
            rect.setOnDragDropped(new EventHandler<DragEvent>() {
                @Override
                public void handle(DragEvent event) {
                    Dragboard db = event.getDragboard();
                    boolean success = false;
                    if (db.hasString()) {
                        rect.setText(db.getString());
                        success = true;
                    }

                    event.setDropCompleted(success);

                    event.consume();
                }
            });

            ansBox.getChildren().add(rect);
        }

        ansBox.getChildren().add(lbl);
        answerGrid.add(ansBox,1,i+1);
    }

    bpane.setCenter(sp);

    //create flowpane for syllables
    FlowPane sylPane = new FlowPane();
    sylPane.setAlignment(Pos.CENTER);
    sylPane.setPrefWidth(200);
    sylPane.setMinWidth(100);
    sylPane.setMaxWidth(300);
    sylPane.setHgap(10);
    sylPane.setVgap(10);

    //create labels for each syllable
    int s = sylList.size();
    for (int i = 0; i<s; i++) {
        //create label for each syllable
        String lblName = sylList.get(i).toString();
        Label lbl = new Label(lblName);
        lbl.setStyle("-fx-background-color: white");
        lbl.setPadding(new Insets(5));
        lbl.setPrefHeight(15);

        //create border for each label
        lbl.setBorder(b);

        //make syllables draggable
        lbl.setOnDragDetected(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                Dragboard db = lbl.startDragAndDrop(TransferMode.MOVE);

                ClipboardContent content = new ClipboardContent();
                content.putString(lbl.getText());
                db.setContent(content);

                event.consume();
            }
        });

        //add to flowpane
        sylPane.getChildren().add(lbl);
    }

    bpane.setRight(sylPane);

    primaryStage.setScene(scene);
    primaryStage.show();
}

0 个答案:

没有答案