tags:

views:

267

answers:

2

I have a JTable where the first column in each row is a checkbox. The user can select and deselect individual columns. I've got this working.

Now I'd like to add to the column header a checkbox which can be used to "select all" and "deselect all". I'm puzzled in how I should go about doing this. I can't work out how to put a component of my choosing in a specific column header.

What can you suggest?

A: 

Ofcourse you can!

Get the tableheader by header = table.getTableHeader(). This returns a component. You can add other components like buttons etc..over this by header.add(btn).

[Edit] Before adding the button you need to set the layout as:

header.setLayout(new BorderLayout()); //if u need someother layout you can set

This is because by defualt the header's layout is null.

Suraj Chandran
It's not that simple, because when that component gets painted, it will no longer be an "active" component (i.e. you can't click it). You must create code to capture the point of the click and pass it on to the checkbox action listeners. See the post (and example through link) by Mark
Tim Drisdelle
@timmyd...no need of such complex code it's unnecessary. I have tried with my code in my answer above with Jcheckbox it works perfectly fine. Please check and remove the down vote.
Suraj Chandran
+2  A: 

Here is an example where someone wanted to do the same thing.

Mark
Thanks, this works a charm, and is highly flexible too.
Steve McLeod