JDK-6524428 : BasicTableUI mouse listener doesn't check mouseEvent.isConsumed()
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2007-02-13
  • Updated: 2011-03-08
  • Resolved: 2011-03-08
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6 JDK 7
6u2Fixed 7 b10Fixed
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
Java(TM) SE Runtime Environment (build 1.6.0-rc-b104)
Java HotSpot(TM) Client VM (build 1.6.0-rc-b104, mixed mode, sharing)

A DESCRIPTION OF THE PROBLEM :
BasicTableUI used to use private shouldIgnore0() method to check if the MouseEvent has been consumed in MouseListener. But in JDK6 it doesn't check any more.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Insets a MouseListener to JTable before all other mouse listeners in the JTable. In mousePressed and mouseReleased, call e.consumed. Now if you click on the JTable, no cell should be selected because the MouseEvent reach our MouseLIstener first and the event is consumed. It worked as expected on JDK5. But in JDK6, it still selects the row when mouse is pressed.

See below for the test case.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
JTable shouldn't be selected as the mouse event is consumed.
ACTUAL -
JTable is selected.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;

public class TestMouseListenerOnTable {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JTable table = new JTable(5, 5);
        insertMouseListener(table, new MouseAdapter(){
            public void mousePressed(MouseEvent e) {
                e.consume();
            }

            public void mouseReleased(MouseEvent e) {
                e.consume();
            }
        }, 0);
        frame.add(new JScrollPane(table));
        frame.pack();
        frame.setVisible(true);
    }

    public static void insertMouseListener(Component component, MouseListener l, int index) {
        MouseListener[] listeners = component.getMouseListeners();
        for (int i = 0; i < listeners.length; i++) {
            MouseListener listener = listeners[i];
            component.removeMouseListener(listener);
        }
        for (int i = 0; i < listeners.length; i++) {
            MouseListener listener = listeners[i];
            if (index == i) {
                component.addMouseListener(l);
            }
            component.addMouseListener(listener);
        }
        // inex is too large, add to the end.
        if (index > listeners.length - 1) {
            component.addMouseListener(l);
        }
    }
}

---------- END SOURCE ----------

Release Regression From : 5.0u10
The above release value was the last known release where this 
bug was not reproducible. Since then there has been a regression.

Comments
EVALUATION In version 5, we only checked isConsumed() as a means to communicate between the listener responsible for drag gesture recognition and the listener responsible for selection. This implementation detail was removed with a consolidation of these listeners in 6 (fix 4521075). That being said, it completely makes sense that we should now ignore mouse events completely if they've been consumed. As such, I'll fix this for table, list and tree.
13-02-2007