首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JTree节点未从DefaultTreeModel中删除

JTree节点未从DefaultTreeModel中删除
EN

Stack Overflow用户
提问于 2014-09-26 09:10:06
回答 1查看 79关注 0票数 0

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

代码语言:javascript
复制
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;
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-09 10:55:30

问题是我忘了添加exportDone.Now,它运行得很好

代码语言:javascript
复制
 @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);
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26055970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档