How to get mouse info while dragging and dropping in Java?

We have a request for a product I'm working on to get more feedback about our drag and drop style. In particular, they would like us to highlight certain areas when the mouse over them during a drag operation (to show where the drop will occur). However, we use TransferHandler to handle both DnD and cut / copy / paste, and based on what we tested, it seems that Swing will not allow us to add another DropTarget to the component (in retrospect, for some obvious reasons!) We tried to go down several paths, none of which brought fruit.

Basically, we would like to get into the middle of dragEnter and dragExit events, while retaining the TransferHandler capabilities to simplify the Cut / Copy / Paste and drop procedure. Does anyone have any examples for this kind of thing? Or is it really very difficult to do in the current DnD setup in Swing?

+4
source share
1 answer

Most Swing ready-made components support this embedded system (JTree, JList, etc.). For these components, you can set the reset mode, and ui will handle the corresponding visual effects.

myJList.setDropMode(DropMode.ON_OR_INSERT); 

For a custom component, you need to process the drawing yourself by overriding paintComponent to draw custom effects, and then adding the PropertyChangeListener property to the dropLocation property, which causes the necessary redraws when necessary. This will often be caused by drag and drop, so you can simply call repaints when a change in the forwarding location actually leads to a change in behavior.

Drop Personnel Rendering Tutorial

+2
source

Source: https://habr.com/ru/post/1344148/


All Articles