Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect mouse click anywhere on window

I've written a JWindow that acts a bit like a fancy menu in my application, popping up when a button is pressed. However, I'd like it to disappear if the user clicks anywhere in the main window. I can of course add a mouse listener to the main window, but that doesn't add it to all the components on the window itself, and looping over all the components seems like a bit of a brute force solution (and can't be guaranteed to work if the components on the window change.)

What's the best way of going about doing something like this?

like image 760
Michael Berry Avatar asked Oct 24 '25 03:10

Michael Berry


1 Answers

Try to use Toolkit.getDefaultToolkit().addAWTEventListener(listener, eventMask). Find eventMask that filters only mouse clicks. This AWT listener is global for whole application, so you can see all events that happen.

like image 160
AlexR Avatar answered Oct 25 '25 23:10

AlexR