This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Detecting JCheckBox changes inside JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Detecting JCheckBox changes inside JTable" Watch "Detecting JCheckBox changes inside JTable" New topic
Author

Detecting JCheckBox changes inside JTable

Lon Allen
Ranch Hand

Joined: May 24, 2001
Posts: 67
I have a JTable with a column of checkboxes. I am trying to figure out how to detect user changes of a checkbox. I am not sure if there is a way to add an actionListener to a checkbox that is embedded in a JTable (I am gathering that there isn't). Has anyone found an easy way to detect checkbox selections when the checkbox is in a table cell?
Lena Solomonik
Greenhorn

Joined: Aug 17, 2001
Posts: 5
I would create an object that will represent each row in your table. One of its instance variables would be a Boolean variable that would represent a checkbox column in your table. Also create access methods for this class (get() and set()). When you call setValueAt() of a class that implements AbstractTableModel (you need to have one), you call set() method for Boolean variable of your object. If you need to detect when value of a checkbox is changed, you call get() method.
I hope it's not too confusing.
Good luck.
Lena.
Lon Allen
Ranch Hand

Joined: May 24, 2001
Posts: 67
Lena,
I guess that I don't quite follow you. I am needing to know when a checkbox was changed, so calling get() for the method you are suggesting I create is all well and good if I know when to call it (I need to be aware that a checkbox state has changed). I already have a setValueAt() method in my model that I overrode to be able to set a checkBox to true or false. What I am needing to catch is when the celleditor for the Boolean columns sets a checkbox to true.
Lon Allen
Lena Solomonik
Greenhorn

Joined: Aug 17, 2001
Posts: 5
Lon,
I think I misunderstood you first time.
You can create a class that extends from MouseAdapter and set it up so it would listen for a mouse click only. That way you can detect when user clicks on a checkbox. You need to pass the class to a table.addMouseListener(mouseAdapter) method.
I had an example of it somewhere, but I can't find it. Sorry
I hope it will help.
Lena.
Lon Allen
Ranch Hand

Joined: May 24, 2001
Posts: 67
Lena,
Yes, that is what I am wanting to do. Only problem is and I don't know if this is a bug or a problem with the mouseListener but what I have found, and the reason I am posting the question here, is that some of the time the mouseListener (mouseAdapter) doesn't detect a mouse click. I have implemented this in my code and on other projects and there are times that the event occurs but the mouseAdapter misses the click. In this case any logic that was in place for detecting changes through the mouseListener gets missed and the event structure gets out of sync.
I know that what I probably need is to add a CellEditorListener to the TableColumn, but I am having problems with that implementation.
Thanks,
Lon
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
If you created your own table model (DefaultTableModel), in the setValueAt() method you can do what you want. This method is called whenever a cell value changes in the table.
Lon Allen
Ranch Hand

Joined: May 24, 2001
Posts: 67
OK. I tried that and am not quite sure how to keep the default implementation of setting the check if I do this. I keep getting a stack overflow when the code runs. How do I call super's setValueAt (in defaultTableModel) that got implemented before I created my overriden version of the method?
Lon Allen
Ranch Hand

Joined: May 24, 2001
Posts: 67
Slap me around alittle! I figured it out. "super." I wasn't having to do this before because I had created an overriden method setValueAt(boolean state, int row, int col) and in this menthod I called setValueAt(Object aValue, int row, int column) which is only defined in the super class (defaultTableModel). When I added the method setValueAt(Object aValue, ...) to my model is when the recursive calls and the infinite loops started. I was getting calls back to my method and not to the super class. Adding super.setValueAt... was the fix. Thanks for the help. BTW: I elliminated the calls I created that has the boolean passed in and just cast them as "new Boolean(state)" so I could call just one overriden method setValueAt() in my model. Cleaned things up quite a bit.

Lon Allen
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Detecting JCheckBox changes inside JTable
 
Similar Threads
JTable
JTable
JTable(!!!)
Jtable column
JTable