views:

62

answers:

2

I've hooked up a mouse listener to a JTable to call some stuff when mouse cursor leaves table's bounds. However, mouseExited() method is also called when the mouse cursor is leaving individual cells of a table. Is it some strange quirk in my code or a bug in Swing?

EDIT: I didn't mention that my table is a subclass of a JTable and not a standard JTable

A: 

Adding mouse listeners to PL&F-heavy components is not a great idea. They often have subcomponents which spoil the party. Mouse events are different from other events in that they bubble up the component hierarchy until they hit a component with a mouse listener (so adding a mouse listener is an intrusive operation). JTable in particular uses renderer to stamp each cell as well as editor components.

(Also subclass compnents such as JTable, or other classes such as Thread, is generally a bad idea.)

Tom Hawtin - tackline
Mouse events can continue to propagate if a mouse listener doesn't consume it. And any specific reasons why you don't recommend extending `JTable`? I can understand why not `Thread`, but `JTable` has much stuff left out (e.g. search, hierarchy). You can add many features by extending table model, column model, header, etc., but I've run into cases where I have to extend JTable itself.
Geoffrey Zheng
A listener doesn't have to call `consume()` to prevent the event bubbling up. Just the presence of a listener will do.
Tom Hawtin - tackline
+1  A: 

Sounds normal to me if you're not checking for event.getSource() == myTable

bemace