This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Does Jtree support Drag and Drop? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Does Jtree support Drag and Drop?" Watch "Does Jtree support Drag and Drop?" New topic
Author

Does Jtree support Drag and Drop?

sandeep kabra
Greenhorn

Joined: Nov 26, 2000
Posts: 10
Is drag and drop support available for Jtree? i.e can I create and delete nodes in Jtree using drag and drop ?
wip siva
Ranch Hand

Joined: Nov 03, 2000
Posts: 36
yes it supports.u have to customize it.we have done it.
i just give u the code.it may helps for u

//Source file: OssDragableTree.java
/********************************************************************************************************
Author Requirements
---------------------------------------------------------------------------------------------------------
Yashwanth.C.P NONE
Description : Generic class for the tree which listens to drag and drop events,to be used in
OSSSmart Configuration and Monitoring GUI
**********************************************************************************************************/

import javax.swing.JTree;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.dnd.DropTargetListener;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragGestureEvent;
import javax.swing.tree.DefaultTreeModel;
import java.awt.dnd.DragSource;
import java.awt.dnd.MouseDragGestureRecognizer;
import java.awt.dnd.InvalidDnDOperationException;
import java.awt.dnd.DropTarget;
import javax.swing.tree.DefaultTreeModel;
import java.awt.datatransfer.*;
import javax.swing.tree.*;
public class OssDragableTree extends OssTree
{
private DragSource objDragSource;
private MouseDragGestureRecognizer objMouseDragGestureRecognizer;
private sysDragGestureListener objDragGestureListener ;
private sysDragSourceListener objDragSourceListener ;
private DropTarget objDropTarget;
public OssDragableTree(DefaultTreeModel treeModel)
{
super(treeModel);
objDragSourceListener = new sysDragSourceListener();
objDragGestureListener = new sysDragGestureListener();
objDragSource = new DragSource();
objMouseDragGestureRecognizer=(MouseDragGestureRecognizer)objDragSource.createDefaultDragGestureRecognizer(this,1, objDragGestureListener);
sysDropTargetListener objDropTargetListener = new sysDropTargetListener();
objDropTarget = new DropTarget(this,objDropTargetListener);
}

class sysDropTargetListener implements DropTargetListener
{
public void dragEnter(DropTargetDragEvent dtde) {}
public void dragExit(DropTargetEvent dte) {}
public void dragOver(DropTargetDragEvent dtde)
{
}
public void drop(DropTargetDropEvent dtde) {}
public void dropActionChanged(DropTargetDragEvent dtde) {}
}
class sysDragSourceListener implements DragSourceListener
{
public void dragDropEnd(DragSourceDropEvent dsde){ }
public void dragEnter(DragSourceDragEvent dsde) { }
public void dragExit(DragSourceEvent dse)
{
treeDragExit(dse);
}
public void dragOver(DragSourceDragEvent dsde){}
public void dropActionChanged(DragSourceDragEvent dsde){}
}
public void treeDragExit(DragSourceEvent dse)
{
}

class sysDragGestureListener implements DragGestureListener
{
public void dragGestureRecognized(DragGestureEvent dge)
{
treeDragGestureRecognized(dge);
}
}
public void treeDragGestureRecognized(DragGestureEvent dge)
{
DragSource objDragSource= dge.getDragSource();
try
{
dge.startDrag(objDragSource.DefaultCopyDrop,new StringSelection("TEST"),objDragSourceListener);
}
catch(InvalidDnDOperationException i)
{
i.printStackTrace();
}

}

public DefaultMutableTreeNode getSelectedNode()
{
TreePath selPath = getSelectionPath();
if(selPath != null)
return (DefaultMutableTreeNode)selPath.getLastPathComponent();
return null;
}

}
sandeep kabra
Greenhorn

Joined: Nov 26, 2000
Posts: 10
Thank-you
Maneesh Grover
Greenhorn

Joined: Dec 30, 2002
Posts: 1
Hi wip siva...
need a small favour frm u ... or anyone who can answer my query ...
In case of drag n drop... whenever i try to drag n drop multiple elements within the same root node.. it doesn't reflect the multiple subnodes..
and at the same time ...
when i delete a node and try to drag another node in his position .. it doesn't reflect .. it does change the icon frm dot to directory with + but doesn't reflect the element within that.
am attaching the sample code of mine.. do let me know whts missing...
am using TreeModel .. not DefaultTreeModel..
If anyone of u can help me it would be great...
Thanks in advance
public void dragGestureRecognized(DragGestureEvent e) {
//Get the selected node
AdapterNode dragNode =(AdapterNode)newPath.getLastPathComponent();;
if (dragNode != null) {
//Get the Transferable Object
Transferable transferable = (Transferable) dragNode.getUserObject();
/* ********************** CHANGED ********************** */
//Select the appropriate cursor;
Cursor cursor = DragSource.DefaultCopyNoDrop;
int action = e.getDragAction();
if (action == DnDConstants.ACTION_MOVE)
cursor = DragSource.DefaultMoveNoDrop;

//In fact the cursor is set to NoDrop because once an action is rejected
// by a dropTarget, the dragSourceListener are no more invoked.
// Setting the cursor to no drop by default is so more logical, because
// when the drop is accepted by a component, then the cursor is changed by the
// dropActionChanged of the default DragSource.
/* ****************** END OF CHANGE ******************** */
//begin the drag
dragSource.startDrag(e, cursor, transferable, this);
}
}
public void dragDropEnd(DragSourceDropEvent dsde) {
}
/** DragSourceListener interface method */
public void dragEnter(DragSourceDragEvent dsde) {
/* ********************** CHANGED ********************** */
/* ****************** END OF CHANGE ******************** */
}
/** DragSourceListener interface method */
public void dragOver(DragSourceDragEvent dsde) {
/* ********************** CHANGED ********************** */
/* ****************** END OF CHANGE ******************** */
}
/** DragSourceListener interface method */
public void dropActionChanged(DragSourceDragEvent dsde) {
}
/** DragSourceListener interface method */
public void dragExit(DragSourceEvent dsde) {
}

public void drop(DropTargetDropEvent e) {
try {
Transferable tr = e.getTransferable();
//cast into appropriate data type
AdapterNode childInfo =
(AdapterNode) tr.getTransferData( AdapterNode.INFO_FLAVOR );

//get new parent node
Point loc = e.getLocation();
//SwingGUI.tree.
TreePath destinationPath = documentTree.getPathForLocation(loc.x, loc.y);
AdapterNode newParent = (AdapterNode)destinationPath.getLastPathComponent();

try {

newParent.addNode(childInfo, document);
// e.rejectDrop();
}
catch (java.lang.IllegalStateException ils) {
System.out.println("Into catch");
e.rejectDrop();
}
// e.getDropTargetContext().dropComplete(true);

System.out.println("full ppath"+destinationPath);
//documentTree.repaint();

}
catch (IOException io) {
System.out.println("Into io exception");
e.rejectDrop(); }
catch (UnsupportedFlavorException ufe) {
System.out.println("Into ufe exception");
e.rejectDrop();}
} //end of method

/** DropTaregetListener interface method */
public void dragEnter(DropTargetDragEvent e) {
}
/** DropTaregetListener interface method */
public void dragExit(DropTargetEvent e) {
}

public void dragOver(DropTargetDragEvent e) {
/* ********************** CHANGED ********************** */
//set cursor location. Needed in setCursor method
Point cursorLocationBis = e.getLocation();
//SwingGUI.tree.
TreePath destinationPath =
documentTree.getPathForLocation(cursorLocationBis.x, cursorLocationBis.y);

// if destination path is okay accept drop...
if (testDropTarget(destinationPath, newPath) == null){
e.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE ) ;
}
// ...otherwise reject drop
else {
e.rejectDrag() ;
}
/* ****************** END OF CHANGE ******************** */
}
/** DropTaregetListener interface method */
public void dropActionChanged(DropTargetDragEvent e) {
}
private String testDropTarget(TreePath destination, TreePath dropper) {
//Typical Tests for dropping
//Test 1.
boolean destinationPathIsNull = destination == null;
if (destinationPathIsNull)
return "Invalid drop location.";
//Test 2.
AdapterNode node = (AdapterNode) destination.getLastPathComponent();
// if ( !node.getAllowsChildren() )
// return "This node does not allow children";
if (destination.equals(dropper))
return "Destination cannot be same as source";
//Test 3.
if ( dropper.isDescendant(destination))
return "Destination node cannot be a descendant.";
//Test 4.
if ( dropper.getParentPath().equals(destination))
return "Destination node cannot be a parent.";
return null;
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Does Jtree support Drag and Drop?