添加到 JTable 的 MouseListener 的奇怪行为
我已将鼠标侦听器连接到 JTable,以便在鼠标光标离开表边界时调用一些内容。但是,当鼠标光标离开表格的各个单元格时,也会调用 mouseExited() 方法。这是我的代码中的一些奇怪的怪癖还是 Swing 中的错误?
编辑:我没有提到我的表是 JTable 的子类,而不是标准 JTable
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不检查
event.getSource() == myTable
,对我来说听起来很正常Sounds normal to me if you're not checking for
event.getSource() == myTable
向 PL&F 密集型组件添加鼠标侦听器并不是一个好主意。他们经常有一些会破坏聚会的子组件。鼠标事件与其他事件的不同之处在于,它们在组件层次结构中向上冒泡,直到它们击中具有鼠标侦听器的组件(因此添加鼠标侦听器是一种侵入性操作)。
JTable
特别使用渲染器来标记每个单元格以及编辑器组件。(此外,子类组件(例如
JTable
)或其他类(例如Thread
)通常也是一个坏主意。)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 asThread
, is generally a bad idea.)