| Author |
Select All checkbox in Jtable
|
Sharad Kharya
Ranch Hand
Joined: Oct 15, 2008
Posts: 58
|
|
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
|
 |
Mansi Mishra
Ranch Hand
Joined: Dec 26, 2008
Posts: 50
|
|
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>
|
http://splashpress.blogspot.com
|
 |
Sharad Kharya
Ranch Hand
Joined: Oct 15, 2008
Posts: 58
|
|
@Mansi
your code seems to be applied for JSP.
I am developing application using Swing API's
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8439
|
|
Sharad Goluone wrote:
Please check your private messages for an important administrative matter.
|
[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
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:
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8439
|
|
|
Why don't you post your code so we can help you figure out what's going wrong.
|
 |
Sharad Kharya
Ranch Hand
Joined: Oct 15, 2008
Posts: 58
|
|
@Manish
code is here
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8439
|
|
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
Joined: Oct 15, 2008
Posts: 58
|
|
Hre the code is
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8439
|
|
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
Joined: Oct 15, 2008
Posts: 58
|
|
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
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8439
|
|
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.
|
 |
Brian Cole
Author
Ranch Hand
Joined: Sep 20, 2005
Posts: 852
|
|
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.]
|
bitguru blog
|
 |
Sharad Kharya
Ranch Hand
Joined: Oct 15, 2008
Posts: 58
|
|
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
Joined: Oct 15, 2008
Posts: 58
|
|
Problem resolved.
Sharing solution for other ranchers who may have the same problem?
~Sharad
|
 |
 |
|
|
subject: Select All checkbox in Jtable
|
|
|