United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4413412 : Mouse Events intercepted by Tooltip

Details
Type:
Bug
Submit Date:
2001-02-08
Status:
Closed
Updated Date:
2006-08-25
Project Name:
JDK
Resolved Date:
2006-08-25
Component:
client-libs
OS:
windows_nt,windows_xp
Sub-Component:
javax.swing
CPU:
x86
Priority:
P4
Resolution:
Not an Issue
Affected Versions:
1.3.0,6
Fixed Versions:
7

Related Reports
Duplicate:
Relates:
Relates:

Sub Tasks

Description
Name: yyT116575			Date: 02/08/2001


java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

When you add a tooltip to a JLabel, you no longer can listen for MouseMotion
events in any Container behind that JLabel.  For example, I added a JLabel to
the JFrame and added a MouseMotionListener to the JFrame.  The JFrame sees the
MouseMotion events even when you move across the label.  The minute you add a
tooltip to the JLabel, the MouseMotion events are not propogated to the JFrame.

The bug report #4234237 incorrectly closed this as not reproducible.  Their
code added the listener to the label, not the JFrame or JPanel.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class MyFrame extends JFrame {

  public MyFrame() {
    JLabel l1 = new JLabel("This is my label");

    /* comment out following line, then MouseEvent will be triggered
     * but not tooltip shows up.
     */
    l1.setToolTipText("tooltip1");

    this.addMouseMotionListener(new MouseMotionListener(){
      public void mouseDragged(MouseEvent e){
        System.out.println("Dragged " + e.getX() + " " + e.getY());
      }
      public void mouseMoved(MouseEvent e){
        System.out.println("Moved "  + e.getX() + " " + e.getY());
      }
    });

    getContentPane().add(l1, BorderLayout.SOUTH);
    setSize(200,200);
    setVisible(true);
  }

  public static void main(String args[]){
    MyFrame m = new MyFrame();
  }
}
(Review ID: 116608) 
======================================================================

                                    

Comments
EVALUATION

This is not bug
To catch mouseEvents for particuar component you shoud attach a listener
to this component (to the JLabel from test case)

For more details please see 
"http://download.java.net/javadesktop/blogs/alexfromsun/2006.06.28/BOF-0204.pdf"
"MouseEvents" section
                                     
2006-08-25
EVALUATION

Contribution forum : https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?forumID=1463&messageID=14940
                                     
2006-08-22
WORK AROUND

Add MouseMoutionListener to all components which should listen such events

or

Use GlassPane 

     getGlassPane().setVisible(true);
     getGlassPane().addMouseMotionListener(new MouseMotionListener(){
      public void mouseDragged(MouseEvent e){
        System.out.println("Dragged " + e.getX() + " " + e.getY());
      }
      public void mouseMoved(MouseEvent e){
        System.out.println("Moved "  + e.getX() + " " + e.getY());
      }
    });

But in this glass pane only will recieve mouse events
other components won't, so first solution is more preferable
                                     
2005-08-11
EVALUATION

That is how it works: initially JLabel doesn't have any MouseMotionListeners and in this case delegates all mouse events to its parent.
Thats why you can see frame listener works.

This is undocumented behaviour and it is not recommended to rely on it

Setting a tool tip internally adds a MouseMotionListener to the label and stops delegating mouse events to the frame.

See "workaround" section
                                     
2005-08-11



Hardware and Software, Engineered to Work Together