rodude123
rodude123

Reputation: 300

Drag and drop functionality with VBox

I am trying to add drag and drop functionality for my JavaFX project. It sort of works but not really at the same time

VBox questions = new VBox();
questions.getChildren().add(createQustionType("long answer"));
questions.setStyle("-fx-border-color: blue;");
root.setCenter(questions);
questions.setOnDragOver(event ->
{
    event.acceptTransferModes(TransferMode.MOVE);
});
questions.setOnDragDropped(event ->
{
    event.setDropCompleted(true);
    questions.getChildren().add(createQustionType("long answer"));
    event.consume();
});

questions.setOnDragDone(event -> {});
VBox sidePanel = new VBox();
root.setLeft(sidePanel);
//other unnecessary code removed for question

String[] types = new String[]{"multiple choice", "long answer", "short answer"};
for (String type : types)
{
    Button btn = new Button(type);
    btn.setOnDragDetected(event ->
    {
        currBtn = (Button) event.getSource();
        event.consume();
    });
    sidePanel.getChildren().add(btn);}

createQuestionType method returns a borderpane and takes in one parameter of string

This is what I have so far and I don't know where I am going wrong because it seems to work when I drag a file from my desktop or documents etc. which I don't want it to do. I want to use the buttons I have added to the sidepanel as that is what it's intended for.

Also, I have been trying to change the cursor when dragging but also failed at that. If someone could show me what I am doing wrong that would be great.

Upvotes: 0

Views: 356

Answers (1)

rodude123
rodude123

Reputation: 300

I am sorry for those who didn't quite understand my question. I will try to phrase my questions better for next time. Anyways I have managed to fix my problem. I realised I had to use the DragBoard and the ClipboardContent Here is the final code I came up with

VBox questions = new VBox();
root.setCenter(questions);
questions.setOnDragOver(event ->
{
    if (event.getGestureSource() == currBtn && event.getDragboard().hasString())
    {
        event.acceptTransferModes(TransferMode.MOVE);
    }
    event.consume();
});
questions.setOnDragDropped(event ->
{
    Dragboard db = event.getDragboard();
    boolean success = false;
    if (db.hasString())
    {
        questions.getChildren().add(createQustionType(db.getString()));
        success = true;
    }
    event.setDropCompleted(success);
    event.consume();
});

questions.setOnDragDone(event ->
{
    System.out.println("Add clean up code");
    if (event.getTransferMode() == TransferMode.MOVE)
    {
        System.out.println("Drag Done");
    }
    event.consume();
});

VBox sidePanel = new VBox();
root.setLeft(sidePanel);
sidePanel.setMinWidth(100);
//sidePanel.setStyle("-fx-background-color: red");
sidePanel.setStyle("-fx-border-color: red; -fx-min-width: 100px;");
sidePanel.setSpacing(10);

String[] types = new String[]{"multiple choice", "long answer", "short answer"};
for (String type : types)
{
    Button btn = new Button(type);
    btn.getStyleClass().add("qBtn");
    btn.setStyle("-fx-border-color: black;");
    btn.setOnDragDetected(event ->
    {
        currBtn = (Button) event.getSource();
        System.out.println("Dragging node");
        Dragboard db = btn.startDragAndDrop(TransferMode.ANY);
        ClipboardContent content = new ClipboardContent();
        content.putString(btn.getText());
        db.setContent(content);

        event.consume();
    });
    sidePanel.getChildren().add(btn);
}

Upvotes: 1

Related Questions