| | |
Drag and Drop headaches
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2004
Posts: 765
Reputation:
Solved Threads: 38
The object I use to create a Transferable object with is the same object I should get on the drop component, right?
All I want to do is drag a row from one JTable onto another JTable. I don't want to copy the data, only send index number of the dragged row. I've hard-coded "5" into the Transferable object for now.
The dragGestureRecognized() method in the DragGestureListener doesn't seem to ever get called. I had it output to the terminal whenever it was called, but it never showed any output. But wouldn't it have to have been called for any of the remaining DnD features to work? When I output the data from the Transferable object in the drop() method, it displays my table data as an HTML table structure; it also shows several supported DataFlavors even though the Transferable that it was supposedly created with supports only 1. It doesn't seem to be using my method to initiate the drag.
Here's the drag code:
And here's the drop code:
Here's the Transferable class if it helps:
All I want to do is drag a row from one JTable onto another JTable. I don't want to copy the data, only send index number of the dragged row. I've hard-coded "5" into the Transferable object for now.
The dragGestureRecognized() method in the DragGestureListener doesn't seem to ever get called. I had it output to the terminal whenever it was called, but it never showed any output. But wouldn't it have to have been called for any of the remaining DnD features to work? When I output the data from the Transferable object in the drop() method, it displays my table data as an HTML table structure; it also shows several supported DataFlavors even though the Transferable that it was supposedly created with supports only 1. It doesn't seem to be using my method to initiate the drag.
Here's the drag code:
Java Syntax (Toggle Plain Text)
class DSListener extends DragSourceAdapter { public void dragEnter(DragSourceDragEvent e) { DragSourceContext context = e.getDragSourceContext(); int action = e.getDropAction(); if ((action & DnDConstants.ACTION_COPY) != 0) context.setCursor(DragSource.DefaultCopyDrop); else context.setCursor(DragSource.DefaultCopyNoDrop); } public void dragDropEnd(DragSourceDropEvent e) { if (e.getDropSuccess() == false) return; int dropAction = e.getDropAction(); if (dropAction == DnDConstants.ACTION_COPY) { } } } final DragSource dragSource = DragSource.getDefaultDragSource(); final DragSourceListener dsListener = new DSListener(); class DGListener implements DragGestureListener { public void dragGestureRecognized(DragGestureEvent e) { Transferable transferable = new TableTransferable(5); e.startDrag(DragSource.DefaultCopyNoDrop, transferable, dsListener); } } DragGestureListener dgListener = new DGListener(); //viewTable is a JTable dragSource.createDefaultDragGestureRecognizer(viewTable, DnDConstants.ACTION_COPY, dgListener);
And here's the drop code:
Java Syntax (Toggle Plain Text)
class DTListener extends DropTargetAdapter { public void drop(DropTargetDropEvent e) { //purposely left out validation for the moment DataFlavor[] flavors = e.getTransferable().getTransferDataFlavors(); DataFlavor flavor = flavors[0]; e.acceptDrop(DnDConstants.ACTION_COPY); Object obj = null; try { obj = e.getTransferable().getTransferData(flavor); } catch(Throwable t) { System.out.println(t); } System.out.println(obj); if (obj == null) { e.dropComplete(false); return; } if (obj instanceof Integer) { System.out.println(obj); e.dropComplete(true); } } } DropTargetListener dtListener = new DTListener(); DropTarget dropTarget = new DropTarget(sourceTable, DnDConstants.ACTION_COPY, dtListener);
Here's the Transferable class if it helps:
Java Syntax (Toggle Plain Text)
import java.awt.datatransfer.Transferable; import java.awt.datatransfer.DataFlavor; /** * Write a description of class TableTransferable here. * * @author (your name) * @version (a version number or a date) */ public class TableTransferable implements Transferable { private Integer index; private static DataFlavor[] supportedFlavors = {new DataFlavor(new Integer(0).getClass(), "Integer-object")}; /** * Constructor for objects of class TableTransferable */ public TableTransferable(Integer i) { index = i; } public TableTransferable(int i) { this(new Integer(i)); } public Object getTransferData(DataFlavor flavor) { return index; } public DataFlavor[] getTransferDataFlavors() { return supportedFlavors; } public static DataFlavor[] getSupportedDataFlavors() { return supportedFlavors; } public boolean isDataFlavorSupported(DataFlavor flavor) { for(int i=0; i<supportedFlavors.length; i++) { if (flavor.getHumanPresentableName().equals(supportedFlavors[i].getHumanPresentableName())) return true; } return false; } }
![]() |
Similar Threads
- drag & drop (Assembly)
- create an app drag & drop controls in asp.net (ASP.NET)
- can't drag & drop in explorer + can't open link in new window (Windows NT / 2000 / XP)
Other Threads in the Java Forum
- Previous Thread: send data with modem
- Next Thread: parsing html
| Thread Tools | Search this Thread |
android api applet application applications array arrays automation balls bank binary bluetooth business chat class classes clear client code codesnippet collections component database db defaultmethod development dice dragging draw ebook eclipse error event exception formatingtextintooltipjava fractal game givemetehcodez graphics gui hql html ide image infinite input integer invokingapacheantprogrammatically j2me java javaprojects jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie numbers openjavafx oracle parameter php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting sql sqlserver state storm string sun superclass swing swt text-file threads time tree windows





