How to listen to child components?

I have a JPanel with an added JLabels grid. I would like MouseListener listen on MouseEvents , but JLabels seems to be in the way, and MouseEvent doesn't fire when clicked in the location where the JLabel is located.

Is there a relient way to listen for MouseEvents child components?

+4
source share
1 answer

MouseEvents are sent to the topmost (in z-order) component that is enabled for them, that has a mouse pointer registered on it, or has internally configured eventMask to handle them. Although, as a rule, JLabel is transparent (and, therefore, the events should reach the base panel), they can get the opacity of the fi event setting a tooltip.

In jdk 7, you can use JLayer to get all (mouse) events delivered to its children. The JLayer documentation says:

JLayer is a good solution if you only need to do the usual coloring of the composite component or catch input events from your subcomponents .

The predecessor for jdk6 is the JXLayer project in SwingLabs. Another option is to use AWTEventListener as described in Rob blog (be careful: not allowed in context with security restrictions)

+8
source

Source: https://habr.com/ru/post/1382430/


All Articles