• Post Reply Bookmark Topic Watch Topic
  • New Topic

Drag and Drop  RSS feed

 
Swamy Vatti
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a droppable tree and a draggable table. When I drag the rows in the table those should be moved to the directory in the tree. The table is listening to the drag. But the tree is not listening to the drop event. I implemented DropListener to the tree and all the methods in it.
Can any of you find out what can be the reason....
here is the source code of the tree and the table.
TREE :
public class ATree extends javax.swing.JTree implements java.awt.dnd.DropTargetListener {
public ATree() {
super();
}
public ATree(java.lang.Object[] value) {
super(value);
}
public ATree(javax.swing.tree.TreeModel newModel) {
super(newModel);
}
public void dragEnter(java.awt.dnd.DropTargetDragEvent dtde) {
System.out.println("DROP ENTER");
dtde.acceptDrag (DnDConstants.ACTION_MOVE);
}
public void dragExit(java.awt.dnd.DropTargetEvent dte) {
System.out.println("DROP EXIT");
}
public void dragOver(java.awt.dnd.DropTargetDragEvent dtde) {
System.out.println(dtde.getLocation());
int x = (int)dtde.getLocation().getX();
int y = (int)dtde.getLocation().getY();
this.setSelectionPath(this.getClosestPathForLocation(x, y));

}
public void drop(java.awt.dnd.DropTargetDropEvent event) {
try {
Transferable transferable = event.getTransferable();
if (transferable.isDataFlavorSupported (DataFlavor.stringFlavor)){
event.acceptDrop(DnDConstants.ACTION_MOVE);
String s = (String)transferable.getTransferData ( DataFlavor.stringFlavor);
event.getDropTargetContext().dropComplete(true);
System.out.println("Got " + s);
}
else{
event.rejectDrop();
}
}
catch (IOException exception) {
exception.printStackTrace();
System.err.println( "Exception" + exception.getMessage());
event.rejectDrop();
}
catch (UnsupportedFlavorException ufException ) {
ufException.printStackTrace();
System.err.println( "Exception" + ufException.getMessage());
event.rejectDrop();
}
}
public void dropActionChanged(java.awt.dnd.DropTargetDragEvent dtde) {}
}

TABLE :
package com.hound.client.common.gui;
/**
* Insert the type's description here.
* Creation date: (6/13/2001 6:03:44 PM)
* @author: Administrator
*/
import java.io.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;

public class DragTable extends javax.swing.JTable implements DragGestureListener, DragSourceListener {
private DragSource dragSource;
public DragTable(javax.swing.table.TableModel dm) {
super(dm);
}
public void dragDropEnd(java.awt.dnd.DragSourceDropEvent dsde) {
}
public void dragEnter(java.awt.dnd.DragSourceDragEvent dsde) {}
public void dragExit(java.awt.dnd.DragSourceEvent dse) {
}
public void dragGestureRecognized(java.awt.dnd.DragGestureEvent dge) {
int[] rows = getSelectedRows();
String str = "GOT : ";
for (int i = 0; i < rows.length; i++)
str = str + getModel().getValueAt(rows[i], 0).toString();
java.awt.datatransfer.StringSelection text = new java.awt.datatransfer.StringSelection(str);
dragSource.startDrag (dge, dragSource.DefaultMoveDrop, text, this);
}
public void dragOver(java.awt.dnd.DragSourceDragEvent dsde) {
}
public void dropActionChanged(java.awt.dnd.DragSourceDragEvent dsde) {}
public void setDragSource(java.awt.dnd.DragSource newDragSource) {
dragSource = newDragSource;
}
}
 
Jagan Mohan Reddy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
If you are not wrong with the code. I think it is a problem of jdk version you are using.Try to use the jdk1.3 version so that the drag and drop feature works.
Jagan.
 
Swamy Vatti
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response...
I run it thru visual age(JDK1.2). When I run this example as a separate application it works fine.... all the events get fired. the problem is when I add this to my actual application and run it. dropover doesnt get fired but dragexit and dragend gets fired. everything is fine on the drag side but not on the drop side.
 
George Thomas
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this happened when I ran a drag and drop as an applet. When I ran the applet inside of a JFrame it worked (the plugin window comes up) but when I ran the applet right on the page (extending JApplet) not all the drop events fired... I still havn't figured this out. One works,the other doesn't.
Georgie.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!