Reputation: 916
I would like to listen for all the drag and drop events on Swing.
One way I managed to listen for drag events was by adding:
final long mask = AWTEvent.MOUSE_EVENT_MASK + AWTEvent.MOUSE_MOTION_EVENT_MASK;
AWTEventListener eventListener = new AWTEventListener() {
public void eventDispatched(final AWTEvent event) {}
};
Toolkit.getDefaultToolkit().addAWTEventListener(eventListener, mask);
but this does not get drop events, not even release events from the mouse when dropping. Is there a way to intercept all drag and drop events in the application, not on each component separately?
Upvotes: 5
Views: 2057
Reputation: 51525
This is not a complete (maybe not even a feasable ;-) solution to your requirement, but might get you started.
As noted, all low-level input events (like mouseEvents, depending on the underlying OS there might be others as well) are swallowed by the dnd-subsystem during a drag. Internally, they are transformed into DragXXEvents which in turn are fired by the DragSource. The dragSource is a singleton that's re-used across all dnd operations. So a way to go might be:
Some snippet:
DragSource source = DragSource.getDefaultDragSource();
DragSourceMotionListener dsml = new DragSourceMotionListener() {
@Override
public void dragMouseMoved(DragSourceDragEvent dsde) {
debug(dsde);
}
};
source.addDragSourceMotionListener(dsml);
// just some logging
protected void debug(DragSourceEvent dsde) {
DragSourceContext context = dsde.getDragSourceContext();
Component source = context.getComponent();
String text = source != null ? source.getName() : "none";
LOG.info(text + " x/y " + dsde.getX() + "/" + dsde.getY());
}
Still a lot a work ahead ...
Upvotes: 2