File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Kludge or NOT Kludge JTable DnD Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Kludge or NOT Kludge JTable DnD" Watch "Kludge or NOT Kludge JTable DnD" New topic

Kludge or NOT Kludge JTable DnD

Jim Crowell

Joined: Mar 27, 2009
Posts: 7
I have an 'AbstractTableModel' subclass that display 3 columns with Drag & Drop processing from a center column cell [Drag] to a right most column cell [Drop]. The Table Model manages an ArrayList<String> component that holds the row, column parameters.

I have a 'TransferHandler' subclass that services the above DnD capability. It contains 'Override' methods for the following methods:
createTransferable; exportDone; canImport; importData;

The Table Model also is designed to insert a new row if the Drop column element has a non-null String. This is accomplished in my 'importData' override method. I add a row of data to the ArrayList and increment the Table Model 'nRow' parameter.

My problem is that when the 'AbstractTableModel' listener invokes my 'exportDone' override method the JTable component derived from the 'exportDone' 'JComponent' argument is still set to the 'Drop' column before the row was added.

I tried selecting the next row of the JTable in the 'importData' override method and in the 'addRow' method of the Table Model to no avail.

I could not find a better way to fix this so I coded what looks like a 'kludge' [to me]. The 'kludge' works fine but I am wondering if there is a better way to manipulate the JTable instance so that the 'exportDone' will be given the adjusted row index.

The 'kludge' is to set a boolean flag [fAddedRow] in my 'addRow' Table Model method and to use that flag in my 'exportData' override method to increment the row iff the flag is set.

The following is my code for the AbstractTableModel and the TransferHandler:

Professional Computer Solutions...
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Kludge or NOT Kludge JTable DnD