JTable with a JCheckBox in a header column
843806Nov 26 2007 — edited Nov 28 2007Hi everybody,
I have implemented a JTable with a JCheckbox in one of the header columns. For this I have used the following code from the Internet for rendering the table cell:
public class CheckBoxColumnHeader extends JCheckBox implements TableCellRenderer, MouseListener {
protected CheckBoxColumnHeader rendererComponent;
protected int column;
protected boolean mousePressed = false;
public CheckBoxColumnHeader(ItemListener itemListener) {
rendererComponent = this;
rendererComponent.addItemListener(itemListener);
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (table != null) {
JTableHeader header = (ExtendedJTableHeader)table.getTableHeader();
if (header != null) {
rendererComponent.setForeground(header.getForeground());
rendererComponent.setBackground(header.getBackground());
rendererComponent.setFont(header.getFont());
header.addMouseListener(rendererComponent);
}
}
setColumn(column);
rendererComponent.setText(value.toString());
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
return rendererComponent;
}
From a technical point of view, the checkbox works as expected. However, I have problems with the graphical layout of the checkbox in the header column:
1) The checkbox seems to be bigger than the height of the header column. It overlays the border lines of the table. When I increase the preferred size of the header row, the checkbox also increases its size and still overlays the table border lines.
2) The checkbox has a diffferent background shading than the other columns in the header.
Any help for solving these two layout problems are greatly appreciated.
Thanks, Walter