• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1016
  • Last Modified:

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?
1 Solution
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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now