JTree only makes leaves dragged

I need to make only JTree draggable leaves, but the following code snippets make each node in the draggable tree:

tree.setDragEnabled(true); 

How can I limit the draggable element to specific information about the node tree as a property of myNode.isLeaf ();

tia jaster

+7
source share
1 answer

This can be done by changing the TransferHandler JTree to return null Transferable to non-leaf nodes.

Here is a quick example:

  JTree tree = new JTree(); tree.setDragEnabled(true); tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); tree.setTransferHandler(new TransferHandler(null) { public int getSourceActions(JComponent c) { return MOVE; } protected Transferable createTransferable(JComponent c) { JTree tree = (JTree) c; DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getSelectionPath().getLastPathComponent(); if (node.isLeaf()) { // TODO create the Transferable instance for the selected leaf } else { return null; } } }); 
+5
source

All Articles