0
我已經創建了一個自定義的DefaultMutableTreeNode.Now我想執行拖放它正在工作的樹很好,但我想要在刪除後刪除節點。 但事情是我可以插入節點到模型中,但不能從模型中刪除。JTree節點沒有從DefaultTreeModel中刪除
public class ORDnd extends TransferHandler {
ObjectNode sourceNode;
ObjectNode destinationParent;
@Override
public int getSourceActions(JComponent c) {
return MOVE;
}
@Override
protected Transferable createTransferable(JComponent source) {
return new TransferableNode((ObjectNode) ((JTree) source).getSelectionPath().getLastPathComponent(), DataFlavors.ORDataFlavor);
}
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
if (!support.isDrop()) {
return false;
}
try {
if (support.isDataFlavorSupported(DataFlavors.ORDataFlavor)) {
sourceNode = (ObjectNode) support.getTransferable().getTransferData(DataFlavors.ORDataFlavor);
} else {
return false;
}
} catch (UnsupportedFlavorException | IOException ex) {
Logger.getLogger(ReusableDnd.class.getName()).log(Level.SEVERE, null, ex);
}
JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
TreePath path = dropLocation.getPath();
if (path == null) {
return false;
}
destinationParent = (ObjectNode) path.getLastPathComponent();
return (destinationParent.isRoot() && sourceNode.isPage()) || (destinationParent.isPage() && sourceNode.isObject());
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (!canImport(support)) {
return false;
}
JTree tree = (JTree) support.getComponent();
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
if (destinationParent.getNode(sourceNode.getText()) == null) {
/** if (support.getSourceDropActions() == MOVE) {
model.removeNodeFromParent(sourceNode);//Not removing the node from the model
} **/
if (support.isDrop() && support.getDropAction() == MOVE)
{
model.removeNodeFromParent(sourceNode);//Working bcoz changed getSourceDropActions to getDropAction
}
model.insertNodeInto(sourceNode, destinationParent, destinationParent.getChildCount());//this is working fine
model.reload(sourceNode);
return true;
}
return false;
}
}