I just wanted to post some code that I recently got working
for the benefit of other programmers who may have encountered
the same problems I did getting this to work. Enjoy!
class CheckBoxTableCellRenderer
extends JCheckBox
implements TableCellRenderer {
Border noFocusBorder;
Border focusBorder;
public CheckBoxTableCellRenderer() {
super();
setOpaque(true);
setBorderPainted(true);
setHorizontalAlignment(SwingConstants.CENTER);
setVerticalAlignment(SwingConstants.CENTER);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected,
boolean hasFocus,
int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
}
else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
if (hasFocus) {
if (focusBorder == null) {
focusBorder = UIManager.getBorder("Table.focusCellHighlightBorder");
}
setBorder(focusBorder);
}
else {
if (noFocusBorder == null) {
noFocusBorder = new EmptyBorder(1, 1, 1, 1);
}
setBorder(noFocusBorder);
}
setSelected(Boolean.TRUE.equals(value));
return this;
}
}
Manish Hatwalne - 04 Feb 2004 07:23 GMT
If you return Boolean as the column class for your JTable model, this will
be done automatically by JTable.
- Manish
> I just wanted to post some code that I recently got working
> for the benefit of other programmers who may have encountered
[quoted text clipped - 46 lines]
> }
> }
Krick - 04 Feb 2004 14:55 GMT
I'm not sure what you mean. Please explain.
> If you return Boolean as the column class for your JTable model, this will
> be done automatically by JTable.
[quoted text clipped - 53 lines]
> > }
> > }
Christian Kaufhold - 04 Feb 2004 15:51 GMT
> class CheckBoxTableCellRenderer
> extends JCheckBox
[quoted text clipped - 6 lines]
> super();
> setOpaque(true);
Do not use setOpaque for buttons.
setContentAreaFilled(true);
> setBorderPainted(true);
> setHorizontalAlignment(SwingConstants.CENTER);
[quoted text clipped - 14 lines]
> setBackground(table.getBackground());
> }
setEnabled(table.isEnabled());
setComponentOrientation(table.getComponentOrientation());
> if (hasFocus) {
> if (focusBorder == null) {
[quoted text clipped - 5 lines]
> if (noFocusBorder == null) {
> noFocusBorder = new EmptyBorder(1, 1, 1, 1);
How do you know the focusBorder has insets (1,1,1,1)?
if (focusBorder == null)
focusBorder = UIManager.getBorder("Table.focusCellHighlightBorder");
if (focusBorder != null)
{
Insets n = focusBorder.getBorderInsets(this);
noFocusBorder = new EmptyBorder(n);
}
}
Christian
Kleopatra - 06 Feb 2004 14:53 GMT
> public Component getTableCellRendererComponent(JTable table, Object value,
> boolean isSelected,
[quoted text clipped - 5 lines]
> setBackground(table.getSelectionBackground());
> }
don't assume table != null. From the api doc:
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, [...]
Parameters:
table - the JTable that is asking the renderer to draw; can be null
DefaultTableCellRenderer is similarly misbehaving - but that's no reason
not to do it correctly :-)
Greetings
Jeanette
Krick - 06 Feb 2004 21:19 GMT
Ok, here's a revised version incorporating all the suggestions people have made...
class CheckBoxTableCellRenderer
extends JCheckBox
implements TableCellRenderer {
Border noFocusBorder;
Border focusBorder;
public CheckBoxTableCellRenderer() {
super();
//setOpaque(true);
setContentAreaFilled(true); // use this instead of setOpaque()
setBorderPainted(true);
setHorizontalAlignment(SwingConstants.CENTER);
setVerticalAlignment(SwingConstants.CENTER);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected,
boolean hasFocus,
int row, int column) {
if(table == null) {
// ???
}
else {
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
}
else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
setEnabled(table.isEnabled());
setComponentOrientation(table.getComponentOrientation());
if (hasFocus) {
if (focusBorder == null) {
focusBorder = UIManager.getBorder("Table.focusCellHighlightBorder");
}
setBorder(focusBorder);
}
else {
if (noFocusBorder == null) {
if (focusBorder == null) {
focusBorder = UIManager.getBorder("Table.focusCellHighlightBorder");
}
if (focusBorder != null) {
Insets n = focusBorder.getBorderInsets(this);
noFocusBorder = new EmptyBorder(n);
}
}
setBorder(noFocusBorder);
}
setSelected(Boolean.TRUE.equals(value));
}
return this;
}
}