MouseEvent
s are contextual to the component that created them. This means that when you release the mouse button, the source of the event is actually the same component you clicked on.
The location information returned by the MouseEvent
is also within the source components context (0x0 is the top left position of the source component).
This means that when you use Component d = SwingUtilities.getDeepestComponentAt(this.pole, me.getX(), me.getY());
, the coordinates are not in the pole
context, but the source Component
s, which likely means that pole
does not contain those coordinates and the method return's null
.
As the JavaDocs says "If parent does not contain the specified location, then null is returned"
You could try translating the MouseEvent
location to the context of pole
using something like...
MouseEvent evt = SwingUtilities.convertMouseEvent(e.getComponent(), e, textField);
or
Point p = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), textField);
But there is still no guarantee that the point will be within the components bounds.
You could further test the location of the point in relation to the pole
using something more like...
Rectangle bounds = textField.getBounds();
bounds.setLocation(0, 0);
if (bounds.contains(p)) {
// Finally...
}
or possibly...
Point p = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), pole);
Component dropPoint = SwingUtilities.getDeepestComponentAt(pole, p.x, p.y);
But this can still return null
if you drag beyond the pole
's rectangle bounds...