jQuery in Action, 2nd edition*
The moose likes Swing / AWT / SWT and the fly likes Control JTable DnD cells? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Control JTable DnD cells?" Watch "Control JTable DnD cells?" New topic
Author

Control JTable DnD cells?

Jim Crowell
Greenhorn

Joined: Mar 27, 2009
Posts: 7
I have developed a 3 column JTable with DnD capability. I use a TransferHandler subclass and an AbstractTableModel subclass. It is working fine.

However, I have some addition requirement to control the JTable DnD cells, e.g. no DnD for column 0, drag only from column 1 and drop only to column 2.

I studied the TransferHandler methods and added code to several of my override methods but I have not been successful yet.

I think that these requirement should be somewhat common but I have not been able to find any similar documentation using Google.

As is my experience, I suspect I am overlooking something basic and I am making my requirements implementation more difficult then it really is.

Any suggestion?

Regards,


Jim
Professional Computer Solutions...
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2249
    
  47
Can you show the code for your transfer handler?
How are you determining which table column you are dropping onto are you using the JTable.DropLocation class?
Jim Crowell
Greenhorn

Joined: Mar 27, 2009
Posts: 7
Tony,
How are you determining which table column you are dropping onto are you using the JTable.DropLocation class?

No I am not using the 'DropLocation' class.
This is the kind of help I needed. I did not know of that class.
I just took a quick look at it and will examine it more after I complete this post.

I need to turn off Column 0, Drag from column 1 only and Drop to column 2 only.
In column 1, ideally, I should turn off some row cells also to avoid end user confusion.

FYI: I am developing on an iMac using OS X 10.6 and I am using Java 1.6.0_37.

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


Thanks,
Jim...
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2249
    
  47
I'm no expert on DND so take all this advice with a pinch of salt.

You need to limit which columns can be dragged from so I think you need to override getSourceActions() to return the type of transfer action available on the currently selected cell of the JTable. The cells which can't be dragged from return NONE.

For dropping the data you need to use the canImport() method to limit where the data can be dropped. The TransferSupport object passed into the method can be used to get the DropLocation object which can then be used to determine where the user is attempting to drop the dragged data.

BTW I take it you are setting the JTable's drop mode to a suitable value and are enabling dragging on the JTable.
Jim Crowell
Greenhorn

Joined: Mar 27, 2009
Posts: 7

You need to limit which columns can be dragged from so I think you need to override getSourceActions() to return the type of transfer action available on the currently selected cell of the JTable. The cells which can't be dragged from return NONE.


The above advise was just what I needed.
The following is the code that I am currently using successfully:
I probably did not need the 'instanceof' if but I use that approach a lot...



For dropping the data you need to use the canImport() method to limit where the data can be dropped. The TransferSupport object passed into the method can be used to get the DropLocation object which can then be used to determine where the user is attempting to drop the dragged data.


I played with this but was unable to use it as directed.
I ended up adding some custom code to both the 'canImport' override and the 'createTransferable' override methods to [I think] resolve my controls for the 'drop' capability.

I'm still tweaking and testing but I think I am close to my objective...


BTW I take it you are setting the JTable's drop mode to a suitable value and are enabling dragging on the JTable.


Yes, the following code is used:


Thank you for the help.
Regards,
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2249
    
  47
I played with this but was unable to use it as directed.

You should be able to cast the DropLocation object to javax.swing.JTable.DropLocation which has methods to return which column, row etc the user is trying to drop the data into.
Jim Crowell
Greenhorn

Joined: Mar 27, 2009
Posts: 7

You should be able to cast the DropLocation object to javax.swing.JTable.DropLocation which has methods to return which column, row etc the user is trying to drop the data into.

Thanks.
I did some analysis of my old method and the 'JTable.DropLocation' implementation
and it looked like either way the correct drop point is there once the mouse button
is released. However, the 'JTable.DropLocation' method seemed to better track the
mouse drag movement.

Below is the revised 'canImport' method with my original method commented out…

I have an additional question if I may.
In my 'getSourceActions' override method I return a "TransferHandler.MOVE" int
iff the Table Model column is > 0 [i.e. == 1 of 2]…

Also my JTable [emissariesTable] setting are as follows:
emissariesTable.setDragEnabled(true);
emissariesTable.setDropMode(DropMode.USE_SELECTION);
emissariesTable.setTransferHandler(new JEmissaryReportTransferHandler());

What i need to accomplish is to have the drag String set to a Null String after the drag String is dropped in the destination column cell…

That is what I thought the DnD MOVE process would do.

To satisfy my requirement, I went and added some code to my TransferHandler.
My JTable is dynamic in that rows are added and removed during run time by my code.
Therefore, the added code was somewhat difficult [for me anyway] and still has a bug to be fixed.

My question is as follows:
Have I overlooked something in my basic DnD JTable settings so that
the MOVE will work as I expected without my "kluge" code?

Here is the JEmissaryReportTransferHandler with the added code to perform the MOVE:

Regards,
Jim...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Control JTable DnD cells?