how to add a mouse listener to a JTable's cell holding a Boolean value rendered as checkbox

…衆ロ難τιáo~ 提交于 2019-12-17 20:23:00

问题


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.


回答1:


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.




回答2:


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); 
    //... 
 }



回答3:


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.




回答4:


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!...



来源:https://stackoverflow.com/questions/6270351/how-to-add-a-mouse-listener-to-a-jtables-cell-holding-a-boolean-value-rendered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!