How do you drag and drop one row in a JTable to another… How do you drag and drop one row in a JTable to another location in the JTable.

Rammohan Meda

We can do this by creating a library of drag and drop-enabled Swing components.

Issues for drag and drop components

  • Starting the drag operation
  • Drag-under feedback
  • Drag-over feedback
  • The drop
  • Transferable
  • The move operation

The table is a view of the data in its model. If each table cell represents an object in the model, you may want to create a table in which the user can drag and drop individual cells. If the table maps each object to a row and the table columns to the object's attributes, you might want the user to be able to drag and drop a table row.

Starting the drag

Check if any edit is in progress, if yes, stop the edit process with javax.swing.JTable.AccessibleJtable.editingStopped()

Check the data under the pointer is draggable.

Drag-under feedback

Select the row under the pointer for drag-under feedback by the methods javax.swing.Jtable.columnAtPoint() and/or javax.swing.Jtable.rowAtPoint() to determine which row or cell the pointer is over.

Drag-over feedback

Check the row and column at the current point.

Save the row and column that you used to create the Transferable object, hence you can prevent dragging and dropping on the same cell.

The drop

Use the model.setValueAt(row, col) method to replace the selection or create a method in the model that allows insertion before or after the selection.

Transferable

Retrieve the Transferable object from the model. Use the selected row to allow the model to determine how to make the Transferable.

The move operation

Use removeNodeFromParent() to remove the node.

Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact