United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6524428 BasicTableUI mouse listener doesn't check mouseEvent.isConsumed()
JDK-6524428 : BasicTableUI mouse listener doesn't check mouseEvent.isConsumed()

Details
Type:
Bug
Submit Date:
2007-02-13
Status:
Closed
Updated Date:
2011-03-08
Project Name:
JDK
Resolved Date:
2011-03-08
Component:
client-libs
OS:
windows_xp
Sub-Component:
javax.swing
CPU:
x86
Priority:
P3
Resolution:
Fixed
Affected Versions:
6
Fixed Versions:

Related Reports
Backport:
Relates:

Sub Tasks

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.
                                     
2007-02-13



Hardware and Software, Engineered to Work Together