Is TreeItem drag and drop supported?

I am currently working with a JavaFx-2 TreeView representing the file system.

I want to enable drag and drop to allow move operations, but it looks like TreeItem . does not include drag event listeners. I was able to implement drag and drop only for the engView object, but it does not work for sub-positions.

Am I missing something or are drag and drop events not supported for TreeItems?

+6
source share
2 answers

The question was answered by Csh on the Oracle forums: https://forums.oracle.com/forums/message.jspa?messageID=10426066#10426066

You need to implement drag on drop on TreeCell.

Write CellFactory as follows:

TreeView<String> treeView = new TreeView<String>(); treeView.setCellFactory(new Callback<TreeView<String>, TreeCell<String>>() { @Override public TreeCell<String> call(TreeView<String> stringTreeView) { TreeCell<String> treeCell = new TreeCell<String>() { protected void updateItem(String item, boolean empty) { super.updateItem(item, empty); if (item != null) { setText(item); } } }; treeCell.setOnDragDetected(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent mouseEvent) { } }); return treeCell; } }); 

If he wants to declare his reputation or add information to his decision, I will change this answer.

+5
source

@Timst's answer is correct, but you have to change the method of the "updateItem" method in the above case, when you cannot set the "TreeItem" graphic and the tree crashes will not work properly (won’t clear the text in the subsets).

just change the method to:

 @Override protected void updateItem(String item, boolean empty) { super.updateItem(item, empty); if (!empty && item != null) { setText(item); setGraphic(getTreeItem().getGraphic()); }else{ setText(null); setGraphic(null); } } 
+2
source

Source: https://habr.com/ru/post/919225/


All Articles