如何将鼠标侦听器添加到包含呈现为复选框的布尔值的 JTable 单元格
我有一个带有自定义模型的 JTable,该模型实现了扩展 AbstractTableModel 的功能。
public abstract class AbstractTable extends AbstractTableModel{
public Class<? extends Object> getColumnClass(int c) {}
}
因为我已经实现了 getColumnClass 方法,所以布尔值会像复选框一样呈现在表中。 我想拦截复选框的状态更改,但不幸的是我无法直接添加鼠标侦听器,因为我没有对复选框本身的引用,它不是我创建的。
如何设置鼠标监听器来拦截复选框状态更改事件?
编辑:
@jzd 答案是正确的。我可以捕捉到 setValue 方法的变化。但我想知道如何实现基于鼠标侦听器的方法。
I have a JTable with a custom model implemented extending AbstractTableModel.
public abstract class AbstractTable extends AbstractTableModel{
public Class<? extends Object> getColumnClass(int c) {}
}
Because I've implemented the getColumnClass method, Boolean values are rendered in the table like checkboxes.
I would like to intercept checkbox's change of status but unfortunately I can't add directly a mouse listener, because I don't have a reference to the checkbox itself, which it isn't created by me.
How can I set a mouse listener to intercept the checkbox status change event?
EDIT:
@jzd answer is correct. I can catch the change in setValue method. But I would like to know how to implement a mouse listener based approach.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在这个可选值的示例中,
setValue()
方法不会被重写,除了更新内部数据结构并触发适当的事件。ValueEditor
扩展AbstractCellEditor
并实现ItemListener
,而ValueRenderer
扩展JCheckBox
。通过这种方式,编辑器可以在编辑器的itemStateChanged()
内监听渲染器的JCheckBox
。附录:添加
CellEditorListener
是另一种方法,此处显示JTree
。请注意,JTable
本身是一个CellEditorListener
。In this example of selectable values, the
setValue()
method is not overridden, except to update the internal data structure and fire the appropriate event.ValueEditor
extendsAbstractCellEditor
and implementsItemListener
, whileValueRenderer
extendsJCheckBox
. In this way the editor can listen to the renderer'sJCheckBox
inside the editor'sitemStateChanged()
.Addendum: Adding a
CellEditorListener
is another approach, shown here forJTree
. Note thatJTable
itself is aCellEditorListener
.我无法抗拒@jzd 的建议,真的不,我认为不是,不能确保我经历
TableMode#setValue
,但基本上有两个选择
1) TableModelListener
2) AFAIK 只有 TableCellEditor#isCellEditable 可以与 JTable 中的 JCheckBox 或 JRadioButton 相关联来执行此操作
I can't resist with @jzd advice really no, I think that not, not ensure me going throught
TableMode#setValue
,but basically there are two options
1) TableModelListener
2) AFAIK only TableCellEditor#isCellEditable can do that in connections with JCheckBox or JRadioButton in JTable
似乎添加鼠标侦听器是一个额外的步骤。我建议拦截模型的
setValue()
方法中的更改。如果您无法更改
setValue()
方法,那么下一个最好的办法是使用CustomEditor
来阻止更改,因为这不是捕获和隐藏鼠标的好方法从默认布尔编辑器中单击 Even。Seems like adding a mouse listener is an extra step. I would suggest intercepting the change in the
setValue()
method of the model.If you can't change the
setValue()
method then the next best thing is aCustomEditor
that will block changes because this is not a good way to catch and hide the mouse click even from the default boolean editor.我遇到了完全相同的问题,而且我也知道您专门要求为复选框编辑器提供鼠标侦听器,但解决方法可能是添加一个
TableModelListener
,如 此处 在“监听数据更改”部分下,并尝试模拟检测到时的行为更改,但如果您想知道鼠标何时位于复选框上或类似的内容 <鼠标的具体操作>,我担心您必须自己实现一个单元格编辑器,它实现这些行为...至少这就是我会做的...谢谢!...
I was having exactly the same problem, and I also know that you asked specifically for a mouse listener to the checkbox editor, but a workarround might be adding a
TableModelListener
as described here under the section "Listening for Data Changes", and try to simulate the behavior when you detect the change, but if you want to know when the mouse is over the checkbox or things like that < specific actions of the mouse >, I'm affraid that you'll have to make yout own implementation of a cell Editor, which implements those behaviors... At least thats what I would do...Grettings!...