如何将鼠标侦听器添加到包含呈现为复选框的布尔值的 JTable 单元格

发布于 2024-11-14 21:13:57 字数 435 浏览 6 评论 0原文

我有一个带有自定义模型的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

银河中√捞星星 2024-11-21 21:13:57

特别是,我想避免将逻辑放入 setValue() 中。

在这个可选值的示例中,setValue()方法不会被重写,除了更新内部数据结构并触发适当的事件ValueEditor 扩展 AbstractCellEditor 并实现 ItemListener,而 ValueRenderer 扩展 JCheckBox。通过这种方式,编辑器可以在编辑器的 itemStateChanged() 内监听渲染器的 JCheckBox

附录:添加 CellEditorListener 是另一种方法,此处显示 JTree。请注意,JTable 本身是一个 CellEditorListener

Particularly, I would like to avoid putting the logic inside setValue().

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 extends AbstractCellEditor and implements ItemListener, while ValueRenderer extends JCheckBox. In this way the editor can listen to the renderer's JCheckBox inside the editor's itemStateChanged().

Addendum: Adding a CellEditorListener is another approach, shown here for JTree. Note that JTable itself is a CellEditorListener.

春夜浅 2024-11-21 21:13:57

我无法抗拒@jzd 的建议,真的不,我认为不是,不能确保我经历 TableMode#setValue

但基本上有两个选择

1) TableModelListener

2) AFAIK 只有 TableCellEditor#isCellEditable 可以与 JTable 中的 JCheckBox 或 JRadioButton 相关联来执行此操作

public boolean isCellEditable(EventObject getEvent) {
    MouseEvent me = (MouseEvent) getEvent;
    JTable table = (JTable) (me.getSource());
    Point point = me.getPoint();
    int column = table.columnAtPoint(point);
    int row = table.rowAtPoint(point);
    Rectangle rec = table.getCellRect(row, column, true); 
    //... 
 }

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

public boolean isCellEditable(EventObject getEvent) {
    MouseEvent me = (MouseEvent) getEvent;
    JTable table = (JTable) (me.getSource());
    Point point = me.getPoint();
    int column = table.columnAtPoint(point);
    int row = table.rowAtPoint(point);
    Rectangle rec = table.getCellRect(row, column, true); 
    //... 
 }
呢古 2024-11-21 21:13:57

似乎添加鼠标侦听器是一个额外的步骤。我建议拦截模型的 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 a CustomEditor that will block changes because this is not a good way to catch and hide the mouse click even from the default boolean editor.

缺⑴份安定 2024-11-21 21:13:57

我遇到了完全相同的问题,而且我也知道您专门要求为复选框编辑器提供鼠标侦听器,但解决方法可能是添加一个 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!...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文