Forum Discussion

chinnappan_paul's avatar
chinnappan_paul
New Contributor
12 years ago

How to drag a cell from a JTable

Hello, I have a situation where i have to drag an item (probably a cell) from the jtable to another object. I tried different topics in your forum and help pages, but cannot find any solutions (may be I have to research more).



Example: I am searching for a patient in the list(Jtable) and after I find the patient, I have to drag and drop the patient to a bed (jpanel object).



In this above example, I was successful in searching the patient using RowIndex and I was able to get the left and top values of jpanel object (bed). However to perform the drag action, I am not sure how will I drag a cell in the row to the jpanel object.



The question is JGrid.Drag(x1,y1, x2, y2) where x2 and y2 value will be the distance of the jpanel object from the cell of jtable, how will i find the x1, y1 value?



I am attaching the screen of my application. In that, I am trying to drag the guy ' NITIN' to a bed #250. i am not sure how can i accomplish this. The point here is the number of rows in the Jtable increases and does not stay the same. So recorded co-ordinates won't help, with respect to the cell.











2 Replies

  • I found another way and already completed the project :)



    1. Find the row and click on the cell.

    2. do mousex and mousey for the cursor.

    3. do window_to_screen to get x,y of the object

    4. Do the same for the destination object and do difference in screen co-ordinates to calculate the distance to be dragged.



    This works for almost all objects.



    Thank you for your support. Have a nice day

  • Hi Paul,


     


    I googled and found out that, probably, you can call the getCellRect method of your JTable control. It returns a rectangle for the needed cell.