aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to change mouse cursor during drag and drop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to change mouse cursor during drag and drop" Watch "How to change mouse cursor during drag and drop" New topic
Author

How to change mouse cursor during drag and drop

Arun Sanker
Ranch Hand

Joined: Mar 21, 2007
Posts: 44
Hi Guys,

Iam performing a Drag and drop from Table1(on top of the figure) to Table2(down in the figure).

see attached figure...

http://www.upload-images.net/imagen/e80060d9d3.jpg

Have implemented the Drag and drop functionality using "Transferable" and "TransferHandler"using the java tutorial

http://java.sun.com/docs/books/tutorial/uiswing/examples/dnd/index.html#ExtendedDndDemo


Now My problem is that ,I want to make the 1st column in Table2(ie: Column2-0) not to accept any drops so that the cursor appears like a "No-Drop" cursor but with selection on the column cell during a drop action.

Also when I move my cursor between "column2-0" and "column2-1",want to to have the "No-Drop" and "Drop" cursor to appear depending on the column.

How can I achieve it using the TransferHandle class.Dont want to go the AWT way of implementing all the source and target listeners on my own.


Have overridded the "CanImort" as follows:

public boolean canImport(JComponent c, DataFlavor[] flavors) {

JTable table = (JTable)c;
Point p = table.getMousePosition();


/* if(p==null)
return false;
int selColIndex = table.columnAtPoint(p);
if(selColIndex==0)
return false;*/

If I execute the above commented code,The "No-Drop" Icon appears in "column2-0",but no cell selection.Also If I move to "column2-1",which is the 1st column,Still get the "No-Drop" Icon there,also with no cell selection.

for (int i = 0; i < flavors.length; i++) {
if ((DataFlavor.stringFlavor.equals(flavors[i]))) {
return true;
}
}
return false;
}


Thanks in advance....
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
The canImport method isn't called very often during the drag operation. You need something that can keep up with the mouse during drag ops. Something like a MouseMotionListener. But dnd events are not sent to these listeners. For j2se 1.5- you are left with finding a way to do this with classes in the java.awt.dnd package. I would investigate a DropTargetListener/Adapter and try working with the dragOver(DropTargetDragEvent dtde) method for this finer-grained cursor control. Using this one extra listener might give you what you want.
In j2se 1.6 we have this information available in the TransferHandler.DropLocation class.

edit: fix format
[ January 19, 2008: Message edited by: Craig Wood ]
Arun Sanker
Ranch Hand

Joined: Mar 21, 2007
Posts: 44
Thanks Craig...Really appreciate for the code.
Thanks...
Arun Sanker
Ranch Hand

Joined: Mar 21, 2007
Posts: 44
Hi Craig,

Iam using j2se 1.5.So I tried to follow your lead by implementing the DragSource and DragTarget.

But I have still problems.As I have said earlier in my post ,I want to Drag and drop from Table1 to table2.

The stumblingblock for me was

[1]Want to change the cursor to change to "NO_DROP" in Table2(Column-0),as I dont want drop in this column.Thats not happening...

[2] Also when a drop is performed in a column other that (column-0) in table 2,the cursor changes to the "move" cursor(Exactly what I want.Thats fine.)But when I move the same drag to Column1 -The cursor doesnt change to the "NO_DROP" cursor...


Have put the code for Table1 and Table2....Guess something has to be done in the "dragEnter" of DragSource Listener ,but unfortunately dont know what....#

Could you give me a hand on this....

Thanks....

Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
One problem with installing a new DropTarget is that it displaces the DropTarget in the TransferHandler. I tried to add a listener to the TransferHandlers DropTarget to avoid getting in its way.
Seems to work okay (I also tried compiling for -source 1.5).
The changes to the DropTest file:

Gives the same behavior as the j2se 1.6/first version of DropTest.
Arun Sanker
Ranch Hand

Joined: Mar 21, 2007
Posts: 44
Thanks craig.As always, that change did the trick....
Really appreciate,for the time you have spend on this.Was actually waiting for your post....

Now I know whom to put forward any swing questions,If I have some...

Thanks once again..
Arun Sanker
Ranch Hand

Joined: Mar 21, 2007
Posts: 44
Hi Craig,

At the end of the day after playing with the program,I have 1 more question left...
Is it possible to prevent drag from a column in the Drag Table so that a NO_dROP cursor appears in that cell,when the user tries to perform a drag from there...

Sorry for the trouble....

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to change mouse cursor during drag and drop