Given a JTable
with a column of type Boolean.class
, the default renderer is a JCheckBox
. It's easy enough to select individual cells based on a user selection, but it may be convenient to select all or none of the check boxes, too. These recent examples mentioned using JCheckBox
in the table header, but the implementation was awkward and unappealing. If I don't need to sort the column, how can I put a well-behaved control in the JTableHeader
?
Addendum: For convenience, I've added my sscce as an answer, but I'd be pleased to accept an answer that addresses the well-behaved aspect of the problem.
There are two parts of the problem (as I see it :-)
Usability: inventing UI-interaction/elements is prone to confusing users. In no particular order:
- the column header title is meant to describe the content of the column, that content description is lost when in replacing it with an action description
- it's not immediately (for me, the dumbest user on earth :-) clear that the header cell has the function of a toggle button. Accidentally clicking it will loose all earlier content state in that column
So even if interaction analysis comes out with a clear we-do-need/want-it,
- action only in-addition to the content
- use a widget that's clearer (e.g. a tri-state checkbox all-de-/selected, mixed content). Also, de-/selecting must both be possible from mixed content. On second thought, a checkbox probably isn't the best choice either, didn't dig further
- minimize the possibility to accidentally (just for me :-) change bulk state, (e.g. by a clear visual separation of an active area - the checkbox icon) from the "normal header" region.
Technical aspects
- TableHeader is not designed for "live" components. Whatever is wanted has to be controlled by ourselves
- examples are around (e.g. JIDE grid supports adding components)
- fiddling with header tends to look unattractive because it's not trivial to change the renderer and at the same time keep the LAF provided appearance