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

4

7

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.

Dismast answered 7/6, 2011 at 19:10 Comment(0)
I
6

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.

Isaacisaacs answered 8/6, 2011 at 0:34 Comment(4)
+1... This has to be the 10th trashgod post that has helped me immensely with Swing this week!Simms
@The111: Glad to help; I've updated the answer to clarify the distinction between editing and updating.Isaacisaacs
In the first example, what is reason for creating two instances of ValueRenderer?Dressy
One is used as a cell renderer; the other serves the cell editor.Isaacisaacs
V
6

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); 
    //... 
 }
Vacationist answered 7/6, 2011 at 21:56 Comment(0)
I
6

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.

Isaacisaacs answered 8/6, 2011 at 0:34 Comment(4)
+1... This has to be the 10th trashgod post that has helped me immensely with Swing this week!Simms
@The111: Glad to help; I've updated the answer to clarify the distinction between editing and updating.Isaacisaacs
In the first example, what is reason for creating two instances of ValueRenderer?Dressy
One is used as a cell renderer; the other serves the cell editor.Isaacisaacs
T
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.

Triboluminescence answered 7/6, 2011 at 19:14 Comment(2)
Yes I could. Anyway I would like to know if it is also possible and how do that, with a mouse listener. I do that way since now. My table model class is a bit complicated. I would like to use mouse listener to keep away the action that I want to perform on status changed, from the table model itself. Particularly I would like to avoid putting the logic inside setValue. thanks anywayDismast
+1 A custom editor and renderer is the right way to go. I've described an example here.Isaacisaacs
B
0

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

Benediction answered 23/7, 2013 at 19:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.