Methods Summary |
---|
public void | dragDropEnd(java.awt.dnd.DragSourceDropEvent dsde)
System.out.println ("dragDropEnd()");
dropTargetNode = null;
draggedNode = null;
repaint();
|
public void | dragEnter(java.awt.dnd.DragSourceDragEvent dsde)
|
public void | dragEnter(java.awt.dnd.DropTargetDragEvent dtde)
System.out.println ("dragEnter");
dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
System.out.println ("accepted dragEnter");
|
public void | dragExit(java.awt.dnd.DragSourceEvent dse)
|
public void | dragExit(java.awt.dnd.DropTargetEvent dte)
|
public void | dragGestureRecognized(java.awt.dnd.DragGestureEvent dge)
System.out.println ("dragGestureRecognized");
// find object at this x,y
Point clickPoint = dge.getDragOrigin();
TreePath path = getPathForLocation (clickPoint.x, clickPoint.y);
if (path == null) {
System.out.println ("not on a node");
return;
}
draggedNode = (TreeNode) path.getLastPathComponent();
Transferable trans = new RJLTransferable (draggedNode);
dragSource.startDrag (dge,Cursor.getDefaultCursor(),
trans, this);
|
public void | dragOver(java.awt.dnd.DropTargetDragEvent dtde)
// figure out which cell it's over, no drag to self
Point dragPoint = dtde.getLocation();
TreePath path = getPathForLocation (dragPoint.x, dragPoint.y);
if (path == null)
dropTargetNode = null;
else
dropTargetNode = (TreeNode) path.getLastPathComponent();
repaint();
|
public void | dragOver(java.awt.dnd.DragSourceDragEvent dsde)
|
public void | drop(java.awt.dnd.DropTargetDropEvent dtde)
System.out.println ("drop()!");
Point dropPoint = dtde.getLocation();
// int index = locationToIndex (dropPoint);
TreePath path = getPathForLocation (dropPoint.x, dropPoint.y);
System.out.println ("drop path is " + path);
boolean dropped = false;
try {
dtde.acceptDrop (DnDConstants.ACTION_MOVE);
System.out.println ("accepted");
Object droppedObject =
dtde.getTransferable().getTransferData(localObjectFlavor);
/*
// dropped on self?
if (droppedObject == draggedNode) {
System.out.println ("dropped onto self");
// can't reject, because we've accepted, so no-op
return;
}
*/
MutableTreeNode droppedNode = null;
if (droppedObject instanceof MutableTreeNode) {
// remove from old location
droppedNode = (MutableTreeNode) droppedObject;
((DefaultTreeModel)getModel()).removeNodeFromParent(droppedNode);
} else {
droppedNode = new DefaultMutableTreeNode (droppedObject);
}
// insert into spec'd path. if dropped into a parent
// make it last child of that parent
DefaultMutableTreeNode dropNode =
(DefaultMutableTreeNode) path.getLastPathComponent();
if (dropNode.isLeaf()) {
DefaultMutableTreeNode parent =
(DefaultMutableTreeNode) dropNode.getParent();
int index = parent.getIndex (dropNode);
((DefaultTreeModel)getModel()).insertNodeInto (droppedNode,
parent, index);
} else {
((DefaultTreeModel)getModel()).insertNodeInto (droppedNode,
dropNode,
dropNode.getChildCount());
}
dropped = true;
} catch (Exception e) {
e.printStackTrace();
}
dtde.dropComplete (dropped);
|
public void | dropActionChanged(java.awt.dnd.DropTargetDragEvent dtde)
|
public void | dropActionChanged(java.awt.dnd.DragSourceDragEvent dsde)
|
public static void | main(java.lang.String[] args)
JTree tree = new DnDJTree();
DefaultMutableTreeNode root = new DefaultMutableTreeNode("People");
DefaultMutableTreeNode set1 = new DefaultMutableTreeNode("Set 1");
DefaultMutableTreeNode set2 = new DefaultMutableTreeNode("Set 2");
DefaultMutableTreeNode set3 = new DefaultMutableTreeNode("Set 3");
set1.add (new DefaultMutableTreeNode ("Chris"));
set1.add (new DefaultMutableTreeNode ("Kelly"));
set1.add (new DefaultMutableTreeNode ("Keagan"));
set2.add (new DefaultMutableTreeNode ("Joshua"));
set2.add (new DefaultMutableTreeNode ("Kimi"));
set3.add (new DefaultMutableTreeNode ("Michael"));
set3.add (new DefaultMutableTreeNode ("Don"));
set3.add (new DefaultMutableTreeNode ("Daniel"));
root.add (set1);
root.add (set2);
set2.add (set3);
DefaultTreeModel mod = new DefaultTreeModel (root);
tree.setModel (mod);
// expand all
for (int i=0; i<tree.getRowCount(); i++)
tree.expandRow (i);
// show tree
JScrollPane scroller =
new JScrollPane (tree,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
JFrame frame = new JFrame ("DnD JTree");
frame.getContentPane().add (scroller);
frame.pack();
frame.setVisible(true);
|