Specifically in regard to a drag and drop operation, how does a button, text box or whatever know that it has been clicked (or vice versa how does the display manager know what it clicked).

I read some about the COM way of dragging and dropping and I think I get the general idea: on MouseDown, detect what was clicked.

While mouse is still down and cursor position moves a certain amount store the data associated with the drag source.

When the MouseUp event fires, drop the data if the mouse cursor is over a drop target. If it is not a drop target then cancel the operation.

You can expand this to the scenario where you click and drag to select multiple files.. how does the UI know what files it selected?

Does the display manager have a calculated pixel range for every element and compare mouse position clicks to that? If so, how does the display manager know this?

If the answer is too long or difficult to explain here, can you point me to some resources that I could read? I understand that you store data on a mouse click event, but that is a higher level "just call these API functions" view of it.

  • 2
  • So the element itself doesn't know that it was clicked? Also, how are the coordinates for the element determined to begin with and how are they stored? The hit testing couldn't occur without them. Aug 12, 2014 at 2:50
  • These things build on top of each other. Think a low-level API that knows shapes and hit-testing. Then those shapes can have events bound to them as they are grouped into controls. Then finally the top level understand series of events. I've worked with making a simple library like this in OpenGL - trying to make a basic one is very enlightening for this type of question. Have you ever tried?
    – J Trana
    Aug 12, 2014 at 4:15
  • No, I haven't. I've only created GUIs with like Java Swing and the like. Aug 12, 2014 at 8:48

1 Answer 1


It is operating system specific. On Linux, the X11 server manage a tree of X-windows (a tree hierarchy of nested rectangular areas). So the X server knows which X-window is concerned by a mouse click.

If you have access to a Linux + Xorg system, try the xev command.

X11 might be replaced by Wayland in the future.

Underneath, the X11 server program itself manage the mouse location (by integrating the relative movement given by the mouse device) and knows in which X-window is the mouse cursor. It transmit that information -mouse movement & cursor entry/exit events- to the application, here your Java Swing application. See also X Window System protocols & architecture & X Window Core Protocol wikipages.

The trend is to use less server-side X-windows, and move most of the area detection to the client side. Some X11 widget toolkits allocate only one toplevel X-window, and manage widgets internally (but older toolkits used one X-window area per widget).

On other OSes I guess that some software layer (Aero on Windows, or some software layer below that) is receiving mouse displacement events and computing in which rectangular area the mouse cursor has to be displayed.

  • 1
    On Windows the built in component manages similar tree of windows as X11 does. Aero is just a compositor on top of that, similar to compositing window manager on top of X11 server.
    – Jan Hudec
    Aug 12, 2014 at 5:00
  • Do you know how things like highcharts.com detect that a data point is being hovered over? I feel like it must be way too inefficient to iterate through every data point to determine if the cursor falls in that data point's region.
    – btse
    Nov 19, 2015 at 3:09
  • @btse: there exist quite efficient algorithms to find which small object is at a given X,Y position, but I don't much these algorithms. Nov 19, 2015 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.