• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Does Jtree support Drag and Drop?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is drag and drop support available for Jtree? i.e can I create and delete nodes in Jtree using drag and drop ?
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank-you
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic