Class ClickThroughLabel

All Implemented Interfaces:
MouseListener, MouseMotionListener, ImageObserver, MenuContainer, Serializable, EventListener, Accessible, MouseInputListener, SwingConstants

public class ClickThroughLabel extends JLabel implements MouseInputListener
ClickThroughLabel is a special JLabel that will retarget all mouse events to specified target component.

For example, you need to paint some text on a JComponent. Usually you can call Java2D paint text method and paint the text. However the other way to do it is to add JLabel to JComponent and JLabel will not only paint the text but also an optional icon which is better. However if you had mouse listener added to JComponent, the mouse listener will not receive any mouse events when mouse clicks on the JLabel. By using this ClickThroughLabel, mouse event will be passed to underlying JComponent.

Please note, we didn't pass all mouse events. In most cases, MOUSE_EXITED and MOUSE_ENTERED doesn't make sense to pass through. However there are cases, for example when the JLabel is at the border of JComponent, you may expect MOUSE_ENTERED event on JComponent but it will not happen. So please be aware of those cases so that you don't depend on it for important decision in your code.

See Also: