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?

Steve McLeod
  • 51,737
  • 47
  • 128
  • 184

3 Answers3

5

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

Mark
  • 28,783
  • 8
  • 63
  • 92
0

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
  • 24,433
  • 12
  • 63
  • 94
  • 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 – Timothy Jan 08 '10 at 10:49
  • @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 Jan 08 '10 at 11:02
0

enter image description here

See here https://stackoverflow.com/a/29963916/1201614 for an example on how to add controls to a table header

Community
  • 1
  • 1
luca
  • 7,178
  • 7
  • 41
  • 55