943,929 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2504
  • Java RSS
Mar 22nd, 2006
0

Drag and Drop headaches

Expand Post »
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:
Java Syntax (Toggle Plain Text)
  1.  
  2. class DSListener extends DragSourceAdapter
  3. {
  4. public void dragEnter(DragSourceDragEvent e)
  5. {
  6. DragSourceContext context = e.getDragSourceContext();
  7. int action = e.getDropAction();
  8. if ((action & DnDConstants.ACTION_COPY) != 0)
  9. context.setCursor(DragSource.DefaultCopyDrop);
  10. else
  11. context.setCursor(DragSource.DefaultCopyNoDrop);
  12. }
  13.  
  14. public void dragDropEnd(DragSourceDropEvent e)
  15. {
  16. if (e.getDropSuccess() == false)
  17. return;
  18.  
  19. int dropAction = e.getDropAction();
  20. if (dropAction == DnDConstants.ACTION_COPY)
  21. {
  22. }
  23. }
  24.  
  25. }
  26.  
  27. final DragSource dragSource = DragSource.getDefaultDragSource();
  28. final DragSourceListener dsListener = new DSListener();
  29.  
  30. class DGListener implements DragGestureListener
  31. {
  32. public void dragGestureRecognized(DragGestureEvent e)
  33. {
  34. Transferable transferable = new TableTransferable(5);
  35. e.startDrag(DragSource.DefaultCopyNoDrop, transferable, dsListener);
  36. }
  37. }
  38.  
  39. DragGestureListener dgListener = new DGListener();
  40. //viewTable is a JTable
  41. dragSource.createDefaultDragGestureRecognizer(viewTable, DnDConstants.ACTION_COPY, dgListener);



And here's the drop code:
Java Syntax (Toggle Plain Text)
  1. class DTListener extends DropTargetAdapter
  2. {
  3. public void drop(DropTargetDropEvent e)
  4. {
  5. //purposely left out validation for the moment
  6.  
  7. DataFlavor[] flavors = e.getTransferable().getTransferDataFlavors();
  8. DataFlavor flavor = flavors[0];
  9. e.acceptDrop(DnDConstants.ACTION_COPY);
  10. Object obj = null;
  11. try
  12. {
  13. obj = e.getTransferable().getTransferData(flavor);
  14. }
  15. catch(Throwable t)
  16. {
  17. System.out.println(t);
  18. }
  19. System.out.println(obj);
  20. if (obj == null)
  21. {
  22. e.dropComplete(false);
  23. return;
  24. }
  25.  
  26. if (obj instanceof Integer)
  27. {
  28. System.out.println(obj);
  29. e.dropComplete(true);
  30. }
  31.  
  32. }
  33. }
  34.  
  35. DropTargetListener dtListener = new DTListener();
  36. DropTarget dropTarget = new DropTarget(sourceTable, DnDConstants.ACTION_COPY, dtListener);


Here's the Transferable class if it helps:
Java Syntax (Toggle Plain Text)
  1.  
  2. import java.awt.datatransfer.Transferable;
  3. import java.awt.datatransfer.DataFlavor;
  4.  
  5. /**
  6.  * Write a description of class TableTransferable here.
  7.  *
  8.  * @author (your name)
  9.  * @version (a version number or a date)
  10.  */
  11. public class TableTransferable implements Transferable
  12. {
  13. private Integer index;
  14. private static DataFlavor[] supportedFlavors = {new DataFlavor(new Integer(0).getClass(), "Integer-object")};
  15.  
  16. /**
  17. * Constructor for objects of class TableTransferable
  18. */
  19. public TableTransferable(Integer i)
  20. {
  21. index = i;
  22. }
  23.  
  24. public TableTransferable(int i)
  25. {
  26. this(new Integer(i));
  27. }
  28.  
  29. public Object getTransferData(DataFlavor flavor)
  30. {
  31. return index;
  32. }
  33.  
  34. public DataFlavor[] getTransferDataFlavors()
  35. {
  36. return supportedFlavors;
  37. }
  38.  
  39. public static DataFlavor[] getSupportedDataFlavors()
  40. {
  41. return supportedFlavors;
  42. }
  43.  
  44. public boolean isDataFlavorSupported(DataFlavor flavor)
  45. {
  46. for(int i=0; i<supportedFlavors.length; i++)
  47. {
  48. if (flavor.getHumanPresentableName().equals(supportedFlavors[i].getHumanPresentableName()))
  49. return true;
  50. }
  51. return false;
  52. }
  53. }
Similar Threads
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: send data with modem
Next Thread in Java Forum Timeline: parsing html





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC