我有一个Jtree,我需要拖放Jtree中的节点。这意味着,我应该能够从jtree中的任何位置拖动节点,并且应该能够将它放在同一Jtree的任何位置。
这是我为节点传输编写的代码
class TreeTransferHandler2 extends TransferHandler
{
DataFlavor nodesFlavor;
DataFlavor[] flavors = new DataFlavor[1];
DefaultMutableTreeNode[] nodesToRemove;
@Override
protected Transferable createTransferable(JComponent c) {
JTree tree = (JTree)c;
Object object = tree.getSelectionPath().getLastPathComponent();
if (object instanceof DefaultMutableTreeNode)
{
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) object;
String toString = selectedNode.getUserObject().toString();
return new StringSelection(toString);
}
return null;
}
@Override
public int getSourceActions(JComponent c) {
return COPY_OR_MOVE;
}
@Override
public String toString() {
return getClass().getName();
}
}