JTable with checkbox header

I have a JTable with a checkbox header. I can't get the checkbox in the header to render correctly. It doesn't check/uncheck when clicked. There are 3 classes: TableWithCheckBoxHeader with nested class CheckBoxHeaderItemListener, and MyHeaderCheckbox. What is the problem?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roger NeymanRetired Software Engineer, Author, TeacherCommented:
For one thing, your code gets an array index out of bounds exception because you've got a table with three rows and you're trying to set (in two places) a checkbox on a fourth row.

See the code I'm suggesting below for fixing up that part to get your code at least functional.  Study the difference in technique carefully.

As for rendering the header checkbox correctly, I suggest you study the following example:


// substitute something like this 
// for both checkAll and uncheckAll
private void setAllCheckBoxes(boolean checked) {
	int rowsCount = tableModel.getRowCount();
	for ( int n=0; n<rowsCount; n++) {
		tableModel.setValueAt(checked, n, 0);

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.