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

前端 未结 4 1353
Happy的楠姐
Happy的楠姐 2020-12-11 06:16

I have a JTable with a custom model implemented extending AbstractTableModel.

public abstract class AbstractTable extends AbstractTableModel{

     public Cl         


        
相关标签:
4条回答
  • 2020-12-11 06:24

    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); 
        //... 
     }
    
    0 讨论(0)
  • 2020-12-11 06:24

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

    0 讨论(0)
  • 2020-12-11 06:28

    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.

    0 讨论(0)
  • 2020-12-11 06:29

    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.

    0 讨论(0)
提交回复
热议问题