Attaching items to a mouse pointer

Hi,

I am working on a project where I can select items from the screen, say, an image of some sorts.

The idea is I will click on this image and a screen will pop up, I will then select from a list on this screen 1 other item or a group of items, and then click OK. When I click OK another image will be created and attached to my mouse pointer. I can then move my mouse around with the image and click at a certain point on the screen to deposit my image there.

I have looked at the java API and under the mouse section (ie: mouse listener, etc), but there doesn’t appear to be anything like this. Is it even possible? I am looking for someone to point me in the right direction, mostly an API, or a tutorial (or sample code).

Thank you !

You could set the mouse cursor to use a different appearance depending on what is attached. Checkout:
Component.setCursor(…).
Toolkit.createCustomCursor(…).

Thanks, I’ll check it out.

There is also drag/drop, but I have to admit I am not too familiar with it …

I can do drag and drop, but I’m not totally sure how to make it stay ‘dragged’ until you click the mouse button again.

I’m not sure I understand all, but when you select something from first menu, then you remember it and create custom cursor with that image until you click somewhere else and draw image that you remembered on that location, changing cursor back to normal.
All this is pretty doable, hardest part is custom cursor I think.

I shall try to explain again.

I’ll use desktop icons as an example. You can click and drag them around and drop them - I can do that in java.

BUT, imagine this. You want to move an icon. So you click it. Then it is attached to your mouse pointer, so your pointer graphic doesn’t change. When you move your mouse around, the clicked icon will now move with the mouse pointer. When you click it again, the icon that is attached to your mouse pointer will be released and placed on a new spot on the desktop.

So instead of “click and drag” we are doing “click and drop”. To move items you don’t have to hold down the mouse bottom anymore.

Does that explain it better?

Your explanation was good! I am not sure if we need a drag & drop expert here?

Another solution would be to add a mouse listener to the window/panel: Add a glass pane or something on top.

  1. you click on the icon, it is removed from the original panel and attached to the glass pane
  2. when you move your mouse, you adjust the position of the icon on the glass pane, so it sits right under the mouse pointer
  3. you click again, you remove the icon from the glass pane and put it into the new panel

Its worth noting the drag&drop api is realy badly designed and in some cases simply doesn’t function as described.
In particular (on windows atleast) the Image parameter for displaying an Image while a drag operation is in effect doesn’t work. (DragSource.startDrag(…))