• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Checkbox editor for JTable column

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have written checkbox editor for JTable column. I am bale to see checkbox and can select or deselect it. But if I select row 1 checkbox and then if click on row 2 checkbox, the selected checkbox of row 1 disappers, it becomes false again. That is I can select only one checkbox. I am not getting what i written wrong in editor. Please someone can help me.

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're making life way too hard for yourself. If you let your TableModel return Boolean.class for that column from getColumnClass, your JTable will automatically use a check box for both editing and rendering. No need to write anything yourself. There is similar support for numbers (Integer, Double, etc), dates (java.util.Date) and images (javax.swing.Icon and java.awt.Image, I believe).
 
Sham Phadtale
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. I tried that. My TableModel returs Boolean.class when I click on checkbox cell. But checkbox remain unselected. Now nothing is happening.
I also tried to set default renderer and editor as follow but result is still same - no cell i can select.



 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And do you modify the value of the cell using the table model's setValue method? Or more importantly, does getValueAt return the right value afterwards?
 
Sham Phadtale
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, i have not done any changess to these methods. I am setting and getting Object type value to these metod. Do I need to update them?
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your getValueAt() method should return a boolean to make the check box selected or deselected.



Your setValueAt() method should set any boolean value to true or false when the checkbox is selected or deselected appropriately. Note that this value will be used in the getValueAt() method to make the check box checked or unchecked.



Note that myDataObject is my custom object where I set if the check box is checked or unchecked using the setSelected() custom method. And SELECTED_COLUMN indicates the check box column
 
Sham Phadtale
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not getting what are you saying. My setValue method is -


and getValueAt method is -


How should i set or get boolena in these methods?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which column # of your table is having the CheckBox? The getValueAt() and setValueAt() methods of the corresponding column should handle and return boolean values appropriately to make the CheckBox checked / unchecked on the screen and to read when the CheckBox is checked / unchecked.
 
Sham Phadtale
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Last column (8th) have checkbox. I am using single getValueAt() and setValueAt() method to set and get values.

The getValueAt() and setValueAt() methods of the corresponding column should handle and return boolean values appropriately to make the CheckBox checked / unchecked on the screen and to read when the CheckBox is checked / unchecked.


Do you mean I need to overload getValueAt() and setValueAt() methods to show boolean values ?
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overloading these methods makes no sense. You must make sure that getValueAt(int row, int column) returns a Boolean representing the current selected state, and setValueAt(Object value, int row, int column) must make sure that getValueAt returns the set Boolean value next time.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have a custom Table Model class that extends AbstractTableModel class? You need to override not overload the two methods.

How are you intend to save the CheckBox selection in your code? Are you using any boolean variable that will register the selection and deselection? (selection - variable turns true; deselection - variable turns false).

See in below code I have modified the getValueAt() method to return true. So the CheckBox will always be checked. Try below getValueAt() and first confirm if all your CheckBoxes are checked. Then replace the true with appropriate variable (or method) that returns the true state of the CheckBox in the given row.



Note that the columnIndex is checked against 7 (Column indices starts from 0)
 
Sham Phadtale
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, overloading is not making any sense.
Yes I have extended AbstractTableModel class.
I have updated methods as follow -


Still no success
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Read my previous post.
 
Sham Phadtale
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you said I tried with retuning hardcoded true value from getValueAt() method, all check boxes are seen selected. But not able to deselect any of the checkbox.
One more thing setValueAt() method is not getting called anytime. What could be possible reasons of this?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I mean to read my earlier post where I have said that column indices start from 0. So you have to check the column number against 7 and not 8.

I just said to make sure getValueAt() is working for the column index #7 (the check box column).

Replace the getValueAt() with your original code -



Modify the column index value to 7.

Same modify the column index value to 7 in the below setValueAt() method. And can you see the setElementAt() method will be called twice even when the col is 8 as you checked?



 
Sham Phadtale
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry, i did not mentioned that I have updated code to check column index to 7 but still setValueAt() method is not getting called. getColumClass() method is also not get called when I click on checkbox. What could be the reason of this?


When I use -

getColumClass() get called.

When I does not specify any renderer/editor getColumnClass is not getting call.

In both cases setValueat() remain uncalled.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does your getColumnClass() method looks like below



Have you by mistake set the Editor and Renderer to any other custom editor or renderers still?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sham Phadtale wrote:
When I use -

getColumClass() get called.


I think you don't need to call setCellEditor() and setCellRenderer() methods at first place if you have your getColumnClass() to return Boolean for the CheckBox column
 
Sham Phadtale
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My getColumnClass method is as -


getClassPARM() returns correct Class type. For creating column I have written one pojo class containing getter/setter to set properties for column.

Yes I am using custom renderer for other columns. But not using custom editor and renderer for Checkbox column.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And your default renderers and editors have a call to super? Its bit difficult for me to guess without seeing your code.
 
Sham Phadtale
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are right. It is very difficult to guess what is going wrong without seeing code.
But as I have very large legacy code and too many dependancies, it is not possible me to copy everything here.

For default renderes/editors i am not calling super, just setting as follow -



Thanku very much for your unconditional help.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the value of i. Is that equals 7. Do you set only the editor and renderer when i = 7. What you set for other columns? Have you by chance overridden the getCellEditor() and getCellRenderer() methods of the JTable class?

I believe your are not using your GenTableCellEditor anymore.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since I am not able to guess where the error is, I will leave you with a demo code to have CheckBox in the table. Also I think you have overridden the isCellEditable() method and make it return true for the CheckBox column. You can try posting a SSCCE for better help next time.

 
Sham Phadtale
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
isCellEditable() method was missing from TableModel.
After adding this, method setValueAt() method is getting call.
setValueAt() method is throwing some exception, they are due to some mistakes during adding data to HashMap _data. After correcting them i hope it should work.

One doubt, when I click on row 1 of table view, I am getting incorrect row value in setValueAt() method.
I am under impression that if I click on row 1, setValueAt() should get row value 1, but it is not happening now.
Which value is set to row in setValueAt() method, and who set it... TableModel?


 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Row indices are similar to column indices... They too start from 0.

You could have just put a System.out.println() to see it when you click a row Just a thought.. no offence
 
Sham Phadtale
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, actualy due to sorting on one column, the row no getting in setVlueAt() is arbitary but it is from the data I am putting in HashMap. Now Data is getting set correct, but still no change in output, checkbox remain unselected after click on it. I have also fire fireTableCellUpdated(row, col);... but no result.
 
Rancher
Posts: 3324
32
 
Sham Phadtale
Ranch Hand
Posts: 75
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I appologize for not telling about posting same question to two different forums. I have not done that intentionaly , from next time I will take crae that this will not happen again.

Thanks for telling me this ontime.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the row no getting in setVlueAt() is arbitary


what is the meaning here. Are you able to select the check box? You said that you were able to after overriding the isCellEditable() method. Are you not able to select the check box after it's getting sorted (to another column that makes its column index to vary)?
 
Sham Phadtale
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Data HashMap I am putting Object, Vector pair. Object is integer which is primary key of one table. Vector contains data in that database row. After table is displayed, sort on column 1 is programatically get called. So value for row no display in table get shufflled and even if i click on first row of table in setValueAt() method I get another row no, but it is valid primary key integer.
Now only problem is that the checkbox on which clicked, is not get selected but some other checkbox is get selected
I guess this is some problem due to data manipulation. Now checkbox get selected/deselected but not those on which I clicked, too much fun
 
Sham Phadtale
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HUrreyyyyyy Problem is Solved.

Row indices starts with zero and the integer values I am using as key for data starts with 1. It was creating problem. Now I am adding 1 to row no which I get at setValueAt() method. It solved the problem. Now I am able to select/deselect the checkboxes.

Thanku very very much John, Rob S. and Rob C. for all your help.


Now I want to do some background work after state change of checkbox. What should be good way to capture checkbox state change event?
 
Rob Camick
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What should be good way to capture checkbox state change event?



What do you mean? You've already been given the answer to this question!!!
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome

Why not just check the state in the setValueAt() method and call custom method based on true / false. I am not sure how to add ItemListener for CheckBox rendered by JTable using Boolean.class
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even if you would be able to attach an ItemListener to that check box, you still wouldn't be able to find out for which row the check box state has changed. Using setValueAt or a TableModelListener is the way to go.
reply
    Bookmark Topic Watch Topic
  • New Topic