How to add a mouse listener to a JTable cell containing a Boolean value displayed as a checkbox

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

public abstract class AbstractTable extends AbstractTableModel{ public Class<? extends Object> getColumnClass(int c) {} } 

Since I implemented the getColumnClass method, Boolean values ​​appear in the table as flags. I would like to intercept the flag status change, but unfortunately I cannot add the mouse listener directly, because I do not have a link to this flag, which it does not create by me.

How to configure a mouse listener to intercept a checkbox change event?

EDIT:

@jzd is the correct answer. I can catch a change to the setValue method. But I would like to know how to implement a mouse-based approach.

+6
source share
4 answers

In particular, I would like to avoid entering logic inside setValue() .

In this example, the setValue() method is not redefined, except for updating the internal data structure and triggering the corresponding event. ValueEditor extends AbstractCellEditor and implements ItemListener , while ValueRenderer continues with JCheckBox . This way, the editor can listen on the JCheckBox renderer inside the itemStateChanged() editor.

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

+6
source

I can’t resist with the @jzd advice really not, I think not, I can’t guarantee that I will go through t TableMode#setValue ,

but basically there are two options

1) TableModelListener

2) Only AFAIK TableCellEditor # isCellEditable can do this in conjunction 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); //... } 
+5
source

Adding a mouse listener seems to be an extra step. I would suggest intercepting the change in the setValue() method of the model.

If you cannot change the setValue() method, then the next best thing is CustomEditor , which will block the changes, because this is not the best way to catch and hide the mouse click even from the default boolean editor.

+3
source

I had exactly the same problem, and I also know that you specifically asked about listening to the mouse in the TableModelListener editor, but you can add a TableModelListener to the TableModelListener , as described here in the section "Listening for data changes" and try to simulate the behavior when a change is detected, but if you want to know when the mouse is above the flag or similar things <specific mouse actions>, I’m afraid that you will have to make your own implementation of the cell editor that implements these behaviors ... At least that’s what I did would...

Grettings! ...

0
source

Source: https://habr.com/ru/post/889988/


All Articles