[英]Drag and drop functionality with VBox
I am trying to add drag and drop functionality for my JavaFX project. 我正在尝试为JavaFX项目添加拖放功能。 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 createQuestionType方法将返回一个边框并接受一个字符串参数
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.
如果有人可以告诉我我在做什么错,那太好了。
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
我意识到我必须使用DragBoard和ClipboardContent,这是我想出的最终代码
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.