• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Select All checkbox in Jtable

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to implement a Select all functionality in Jtable column header.

for reference, I refer code from here

While working with code, requirement implemented fine, but there is an issue with this code..

Case-
When user clicks on "Check All", all the checkboxes gets selected, But if user un select any of the checkbox from table then ideally "check all" shud gets unselected. But it still remains selected.

please help me on this, Sso I get my things done asap.

Thanks In Advance
Sharad
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Sharad

You could try this javascript function.


function checkAllOrNone() {
var selectAll = document.getElementsByName("formName:tablename:selectAllCheckBoxintheheader");
var rowCount = document.getElementById("formName:tableName").rows.length-2;
if (selectAll[0].checked) {
var i=0;
for(i=0;rowCount>0 && i<rowCount;i++){
document.getElementById("formName:tableName:"+i+":individualcheckboxforeachrow").checked=true;
}
} else{
var i=0;
for(i=0;rowCount>0 && i<rowCount;i++){
document.getElementById("formName:tableName:"+i+":individualcheckboxforeachrow").checked=false;
}
}
}


Mansi>
 
Sharad Kharya
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Mansi

your code seems to be applied for JSP.
I am developing application using Swing API's
 
Sheriff
Posts: 22857
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use a TableModelListener to listen for changes in the table model. Keep in mind that this could cause an infinite loop: checking the check box will change multiple table values, all of which will trigger a check box change. Which could change multiple table values again, etc.

One way to prevent this is only to change the selected state / table model value if it should be changed:
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you post your code so we can help you figure out what's going wrong.
 
Sharad Kharya
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Manish

code is here
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see any code where the header check box will be de-selected if the user de-selects any checkbox in the table.
What have you tried so far?
 
Sharad Kharya
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hre the code is
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question still stands.
Where in the code you have posted, are you handling deselection of check boxes on the table?
 
Sharad Kharya
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manish,

Thats my problem is.
I am explaning the complete scenario once again.

I am using header renderer.

Say in a table i am having 5 rows. So if user selects "select all" check box then all the 5 check boxes gets selected.
and again if user de selects the header checkbox(select all) then all the 5 check boxes gets deselected.

but,
if user selects header checkbox then all the checkboxes gets selected.
After this user deselect any one checkbox from table rows. in this case still header checkbox is shown as selected.

My requirement is, if user deselects any of the checkbox from table rows, then header checkbiox should automatically gets deselected.

Hope, this is the correct information for what you are asking for

Thanks
Sharad

 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sharad,
I understood your question the very first time. Thank you.
Looks like you missed my point. This might help you understand me better.
http://faq.javaranch.com/java/ShowSomeEffort

In pseudo code
1) You need a listener which will help you figure out if the user has clicked any of the table check boxes
2) Then you need to figure out what is the state of the other check boxes in the table
3) If the click has resulted in unchecking the check box in the table, you need to uncheck the checkbox in the header too.

 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:You could use a TableModelListener to listen for changes in the table model. Keep in mind that this could cause an infinite loop



I would tend to implement this in the setValueAt() method of the table model. That may (or may not) be simpler than using a table model listener.

[edit: If the poster is worried about checkboxes in the table header, which I hadn't realized, then this approach is no good.]
 
Sharad Kharya
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Brain,

Can you please post some pseudo code to implement this.
I am not able to make my application work.



with this listener SOP is printed on console whenever i click on any checkbox of table data.
But, I stuck in getting control/modify header component from table listener.

Or even I try to implement this in setValueAt() method then also i need logic to implement this.


Maneesh wrote
3) If the click has resulted in unchecking the check box in the table, you need to uncheck the checkbox in the header too.


Maneesh, I implemented first 2 points but didnt figure out, how to implement the third one.

Please help me, If any one is having sample code for this..

Thanks In Advance
Sharad
 
Sharad Kharya
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem resolved.
Sharing solution for other ranchers who may have the same problem?


~Sharad
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used the above code for the first column of my JTable which contains checkboxes. I've set the project UI look&feel as System Look & Feel. But the select all checkbox header displays different than the other header elements after implementing this code. I'm new to Java so can you please explain how to give this custom checkbox header the same UI?
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
- TableModelListener isn't designated to notify the model itself, never, don't to use for modify value in model, to override setValueAt instead (don't forget fot super.setValue...)

- JTableHeader isn't designated for XxxTableCellEditor, even this is simple, possible, another with mouse events, with good explanations about side effects
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic