001: /*
002: * @(#)ClickThroughLabel.java 2/28/2006
003: *
004: * Copyright 2002 - 2006 JIDE Software Inc. All rights reserved.
005: */
006:
007: package com.jidesoft.swing;
008:
009: import javax.swing.*;
010: import javax.swing.event.MouseInputListener;
011: import java.awt.*;
012: import java.awt.event.MouseEvent;
013:
014: /**
015: * <tt>ClickThroughLabel</tt> is a special JLabel that will retarget all mouse events to specified target component.
016: * <p/>
017: * For example, you need to paint some text on a JComponent. Usually you can call Java2D paint text method and paint
018: * the text. However the other way to do it is to add JLabel to JComponent and JLabel will not only paint
019: * the text but also an optional icon which is better. However if you had mouse listener added to JComponent, the mouse
020: * listener will not receive any mouse events when mouse clicks on the JLabel. By using this <tt>ClickThroughLabel</tt>,
021: * mouse event will be passed to underlying JComponent.
022: * <p/>
023: * Please note, we didn't pass all mouse events. In most cases, MOUSE_EXITED and MOUSE_ENTERED doesn't make sense to pass through.
024: * However there are cases, for example when the JLabel is at the border of JComponent, you may expect MOUSE_ENTERED event
025: * on JComponent but it will not happen. So please be aware of those cases so that you don't depend on it for important
026: * decision in your code.
027: */
028: public class ClickThroughLabel extends JLabel implements
029: MouseInputListener {
030:
031: private Component _target;
032:
033: public ClickThroughLabel() {
034: installListeners();
035: }
036:
037: public ClickThroughLabel(Icon image) {
038: super (image);
039: installListeners();
040: }
041:
042: public ClickThroughLabel(Icon image, int horizontalAlignment) {
043: super (image, horizontalAlignment);
044: installListeners();
045: }
046:
047: public ClickThroughLabel(String text) {
048: super (text);
049: installListeners();
050: }
051:
052: public ClickThroughLabel(String text, int horizontalAlignment) {
053: super (text, horizontalAlignment);
054: installListeners();
055: }
056:
057: public ClickThroughLabel(String text, Icon icon,
058: int horizontalAlignment) {
059: super (text, icon, horizontalAlignment);
060: installListeners();
061: }
062:
063: public Component getTarget() {
064: return _target;
065: }
066:
067: public void setTarget(Component target) {
068: _target = target;
069: }
070:
071: protected void installListeners() {
072: addMouseListener(this );
073: addMouseMotionListener(this );
074: }
075:
076: protected void uninstallListeners() {
077: removeMouseListener(this );
078: removeMouseMotionListener(this );
079: }
080:
081: public void mouseClicked(MouseEvent e) {
082: JideSwingUtilities
083: .retargetMouseEvent(e.getID(), e, getTarget());
084: }
085:
086: public void mousePressed(MouseEvent e) {
087: JideSwingUtilities
088: .retargetMouseEvent(e.getID(), e, getTarget());
089: }
090:
091: public void mouseReleased(MouseEvent e) {
092: JideSwingUtilities
093: .retargetMouseEvent(e.getID(), e, getTarget());
094: }
095:
096: public void mouseEntered(MouseEvent e) {
097: JideSwingUtilities
098: .retargetMouseEvent(e.getID(), e, getTarget());
099: }
100:
101: public void mouseExited(MouseEvent e) {
102: JideSwingUtilities
103: .retargetMouseEvent(e.getID(), e, getTarget());
104: }
105:
106: public void mouseDragged(MouseEvent e) {
107: JideSwingUtilities
108: .retargetMouseEvent(e.getID(), e, getTarget());
109: }
110:
111: public void mouseMoved(MouseEvent e) {
112: JideSwingUtilities
113: .retargetMouseEvent(e.getID(), e, getTarget());
114: }
115: }
|