Hide Panel when clicking outside of it
I have a Swing application that is composed of a 30x30 grid of objects extending JLabels, call it ExtendedJLabel. When you click an ExtendedJLabel a panel appears which lets you edit the properties of the ExtendedJLabel. I have a save and cancel button which work well enough to close the panel, but I wanted to include the functionality so that if the user clicks anywhere else outside the panel (but still inside the application) the panel will be hidden, and the user will again have the opportunity to interact with the grid.
I could of course put a click event handler on each ExtendedJLabel to close the panel if it is open, but this seems ugly and I also have in my application above the 30x30 grid a panel containing some other menu type objects likes scrollpanes and JButtons and JLabels etc. and I would have to put the same closing logic for the panel inside all those as well.
I was hoping there was some kind of way to do a loseFocus event handler on my panel to keep logic in the same place. But my attempts have not been successful. Does anyone have any ideas for achieving this behavior?