Is there a way to do drag and drop in my Jtree?

100 Views Asked by At

I have a Jtree and I need to drag and drop the nodes within the Jtree. That means, I should be able to drag node from any position in jtree and should be able to be drop it in any position that same Jtree.

This is the code I wrote for node transfer

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

}

1

There are 1 best solutions below

0
On

In case you are not talking about window docking:

Yes, this is easy. Netbeans Platform is just an application framework for Swing. You can use all the standard Swing DnD techniques.

  • Maybe you should read into a drag and drop tutorial like that.
  • For dropping files on the application you could read this.