Two JTable examples at http://www2.gol.com/users/tame/swing/examples/JTableExamples2.html include a JCheckBox cellRenderer in a JTable; take a look.
When you say "remove the border around JCheckBox", do you mean remove the square so that only the checkmark is shown? That square is part of an icon, so to get rid of it, you can create your own icon class by copying and edit code from private inner class CheckBoxIcon in class javax.swing.plaf.Metal.MetalIconFactory. I did this and it worked fine.
The standard JCheckBox class includes constructors that take an Icon. An example of how Sun wrote an Icon can be found in the (fairly simple) source code of inner class CheckBoxIcon in class javax.swing.plaf.Metal.MetalIconFactory (see src.jar/src/javax/swing/plaf/metal/MetalIconFactory.java in your JDK installation directory). Based on this code, write your own class that implements Icon and has your desired GUI (the checkbox but not the square around it). Then use this icon when constructing the JCheckBox in your TableCellRenderer, and you should be all set.
Sorry, I still have a problem.
I have written my own Icon class and the paintIcon method as following:
public void paintIcon(Component c, Graphics g, int x, int y) {
JCheckBox cb = (JCheckBox)c;
ButtonModel model = cb.getModel();
int controlSize = getControlSize();
boolean drawCheck = model.isSelected();
if ( model.isEnabled() )
{
if (model.isPressed() && model.isArmed())
{
g.setColor( MetalLookAndFeel.getControl() );
g.fillRect( x, y, controlSize-1, controlSize-1);
}
g.setColor( MetalLookAndFeel.getControl() );
} else
{
g.setColor( MetalLookAndFeel.getControl() );
g.drawRect( x, y, controlSize-1, controlSize-1);
}
if (model.isSelected()) {
drawCheck(c,g,x,y);
}
}
But the model.isSelected() method will always return false, how could I get the actual bool value of table's cell? My cell renderer code is as following:
public class BooleanCellRenderer extends JCheckBox implements TableCellRenderer
{
public BooleanCellRenderer()
{
super(new CheckBoxNoIcon(),true);
}
public Component getTableCellRendererComponent(JTable table, Object checkBox,boolean isSelected, boolean hasFocus,int row, int column)
{
return this;
}
}