我已按照本论坛其他主题的建议实现了拖放功能。我在cellFactory
上有一个TreeView
,并在单元格上设置了事件。
tvProject.setCellFactory(new Callback<TreeView<PlanningItem>, TreeCell<PlanningItem>>() {
@Override
public PlanningCheckBoxTreeCell call(TreeView<PlanningItem> siTreeView) {
final PlanningCheckBoxTreeCell source = new PlanningCheckBoxTreeCell();
final PlanningCheckBoxTreeCell target = new PlanningCheckBoxTreeCell();
source.setOnDragDetected(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
/* drag was detected, start drag-and-drop gesture */
System.out.println("onDragDetected");
/* allow any transfer mode */
Dragboard db = source.startDragAndDrop(TransferMode.ANY);
/* put a string on 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 */
System.out.println("onDragOver");
...
event.consume();
}
});
setOnDragDetected
根据需要执行/命中,但其他所有事件均未执行(我没有在此处发布所有事件:setOnDragOver
,setOnDragEntered
,setOnDragExited
,{{ 1}},setOnDragDropped
)。
setOnDragDone
是树单元的自定义实现,如下所示:
PlanningCheckBoxTreeCell
更新 public class PlanningCheckBoxTreeCell extends CheckBoxTreeCell<PlanningItem> {
public PlanningCheckBoxTreeCell() {
}
@Override
public void updateItem(PlanningItem item, boolean empty) {
super.updateItem(item, empty);
if (!empty) {
setText(null);
}
}
}
:
PlanningItem
更新 MCVE:
package at.v2c2.testplaygroundui;
import java.io.Serializable;
public class PlanningItem implements Serializable {
private static final long serialVersionUID = 1L;
private Double scene = null;
private Integer path = null;
private String move = null;
/**
* @return the scene
*/
public Double getScene() {
return scene;
}
/**
* @param scene the scene to set
*/
private void setScene(Double scene) {
this.scene = scene;
}
/**
* @return the path
*/
public Integer getPath() {
return path;
}
/**
* @param path the path to set
*/
private void setPath(Integer path) {
this.path = path;
}
/**
* @return the move
*/
public String getMove() {
return move;
}
/**
* @param move the move to set
*/
private void setMove(String move) {
this.move = move;
}
public PlanningItem(Object item) {
super();
if (item instanceof Double) {
setScene((Double) item);
} else if (item instanceof Integer) {
setPath((Integer) item);
} else if (item instanceof String) {
setMove((String) item);
}
}
}
预先感谢