繁体   English   中英

未从DefaultTreeModel中删除JTree节点

[英]JTree node not removed from DefaultTreeModel

我已经创建了一个自定义的DefaultMutableTreeNode.Now,我想在树上执行拖放操作,但是拖放后要删除该节点。 但问题是我可以将节点插入模型,但不能从模型中删除。

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;
}
}

事情是我忘了添加exportDone.Now可以正常工作了

 @Override
protected void exportDone(JComponent source, Transferable data, int action) {
    if (action != MOVE) {
        return;
    }
    DefaultTreeModel model = (DefaultTreeModel) ((JTree) source).getModel();
    try {
        model.removeNodeFromParent((ObjectNode) data.getTransferData(DataFlavors.ORDataFlavor));
    } catch (UnsupportedFlavorException | IOException ex) {
        Logger.getLogger(ORDnd.class.getName()).log(Level.SEVERE, null, ex);
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM