This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to change the icon label in a drag and drop  RSS feed

 
Gary W. Lucas
Ranch Hand
Posts: 65
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would anyone have suggestions on how to fix a problem I am having with an icon label when I perform a drag and drop onto a JPanel?

I have an application in which I want to let a user drag an image file into a JPanel and then perform some data processing on it. It works fine except for one thing. When I click on an image in Windows explorer and drag it into the JPanel, the label under the image is "->MOVE". I think this would be a bit disconcerting to a user who might fear I am going to move his image file out of the folder where he has it stored, so I'd prefer it said "+COPY". I've tried a lot of things but can't get it to work that way. I think it must be possible because I've noticed that when I drag the image over Netbeans, the icon is labeled COPY.

I thought that the label might be based on the argument passed to the DropTargetDragEvent method acceptDrag() in the dragEnter() listener, but that doesn't seem to be the case. Does anyone have recommendations?

I am running Java 1.8.0_66 under Windows 7.

Sample code follows:
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disclaimer: I haven't worked much on Java DnD

Looks like it is the expected behavior
Typically, an ordinary drag requests the MOVE action. Holding the Control key while dragging requests the COPY action, and holding both Shift and Control requests the LINK action.

https://docs.oracle.com/javase/tutorial/uiswing/dnd/intro.html
 
Gary W. Lucas
Ranch Hand
Posts: 65
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Maneesh. I find the whole Drag-N-Drop implementation difficult to follow. Perhaps the fact that it has to support so many different things has led to a complicated API... But really, I get the feeling that there are just fundamental concepts I don't understand that would clear the whole thing up if I knew why they did some of the things they do. In any case, I wonder if my whole approach is the right way to use the API or if I'm somehow misusing the tools.

It turns out that if the user does use the appropriate drag gestures (pressing the control key), the icon is marked "COPY" rather than "MOVE". But I'd prefer not to require that of him... The idea behind this is that the application doesn't move the file, it copies it. And if the user doesn't push down the control key, I want the application to give him the reassurance that it won't move his file someplace where he'll never be able to find it afterward. I know it must be possible somehow, because Netbeans does it.

Any idea?
 
Gary W. Lucas
Ranch Hand
Posts: 65
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well, four months later and I've finally figured it out.

The issue was that I label on the drag-and-drop icon was stuck to "MOVE" and I didn't seem to be able to make it read "COPY" or "LINK", both of which would be more appropriate for the application I was dealing with.

It turns out that Swing actually was changing the label in the dragEnter method call... But as soon as the user moved the mouse and dragOver() was invoked, it reverted right back to "MOVE". The fix was to put a call to dtde.acceptDrag(DnDConstants.ACTION_COPY); in the dragOver method (with the proper supporting logic, of course). So if I just lugged the guts of dragEnter into dragOver, the application did just what I wanted.

g
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!