The border around JCheckBox

Hi,Who can tell me that how to remove the border around JCheckBox? My JCheckBox is the default renderer for boolean type data of JTable. Thank you.Janet
[187 byte] By [kfsong] at [2007-9-26 2:35:18]
# 1
Did setBorder(null) not work?
earljavadev at 2007-6-29 10:01:22 > top of Java-index,Archived Forums,Swing...
# 2
Thank you for your reply.I have tried your method, but didn't work. this is my code:((JCheckBox)table.getCellRenderer(4,4)).setBorder(null);
kfsong at 2007-6-29 10:01:22 > top of Java-index,Archived Forums,Swing...
# 3

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.

earljavadev at 2007-6-29 10:01:22 > top of Java-index,Archived Forums,Swing...
# 4
Hi,Yes, I want to remove the square box.Do you mean I have to implement my own CellRenderer? How you did it? Could you give me some detail?Thank you very much.
kfsong at 2007-6-29 10:01:22 > top of Java-index,Archived Forums,Swing...
# 5

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.

earljavadev at 2007-6-29 10:01:22 > top of Java-index,Archived Forums,Swing...
# 6
Thank you so much for your help! I have solved it.Janet
kfsong at 2007-6-29 10:01:22 > top of Java-index,Archived Forums,Swing...
# 7

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;

}

}

kfsong at 2007-6-29 10:01:22 > top of Java-index,Archived Forums,Swing...
# 8
The getTableCellRendererComponent method receives the argument you need: boolean isSelected.
earljavadev at 2007-6-29 10:01:22 > top of Java-index,Archived Forums,Swing...
# 9
I have tested, the isSelected value received by getTableCellRendererComponent() is always "false" too. By the way, when will this method be triggered?
kfsong at 2007-6-29 10:01:22 > top of Java-index,Archived Forums,Swing...