22

I have my on custom cell renderer and want to remove the border of the cell.
How can i do it? I tried setBorder but it doesn't work.

Here is my renderer code:

public class MyTableCellRenderer extends DefaultTableCellRenderer {

    private static final long serialVersionUID = -1195682136616306875L;

    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        Component c = super.getTableCellRendererComponent(table, value,
                isSelected, hasFocus, row, column);
        if (!isSelected) {
            if (row % 2 == 0 && row != 1) {
                c.setBackground(new Color(255, 255, 150));
            } else {
                c.setBackground(Color.WHITE);
            }
        } else {
            c.setBackground(new Color(255, 230, 255));
        }
        c.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        return c;
    }
}
almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
harshit
  • 7,925
  • 23
  • 70
  • 97

2 Answers2

54

The lines drawn between cells are not part of the cells themselves. They are drawn by the table. You can turn them off for the entire table with:

table.setShowGrid(false);

To disable just the the horizontal or just the vertical lines:

table.setShowHorizontalLines(false);
table.setShowVerticalLines(false);

Or, you can change the color of the lines with:

table.setGridColor(color)
Devon_C_Miller
  • 16,248
  • 3
  • 45
  • 71
  • 2
    You can do both at the same time with the [setShowGrid()](http://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html#setShowGrid(boolean)) as well – Wim Deblauwe Sep 06 '12 at 08:35
  • @WimDeblauwe Thanks, I missed that one – Devon_C_Miller Sep 06 '12 at 13:57
  • @Devon_C_Miller What if I want to remove border around 1 cell only?? – Nikhil Chilwant Feb 19 '14 at 05:05
  • @Nikhil The javax.swing.plaf.basic.BasicTableUI which handles the rendering of the grid implements it as a private method, so there's no trivial way to override it. I have not tried these , but some possibilities to look at: https://code.google.com/p/spantable http://www.java2s.com/Code/Java/Swing-Components/MultiSpanCellTableExample.htm – Devon_C_Miller Feb 20 '14 at 18:35
3

I don't know how your code compiles since only Swing components can have a Border and the Component class doesn't have a setBorder() method.

When I override the default renderer I use something like:

Class CustomRenderer extends DefaultTableCellRenderer
{
    public Component getTableCellRendererComponent(
        JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        this.setBorder (BorderFactory.createBevelBorder (EtchedBorder.RAISED));
        return this;
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1
    I am able to set all type of Borders except the emptyBorder. – harshit Jul 02 '10 at 16:37
  • 2
    And what do you expect to see when you use an EmptyBorder? How do you know its not working. It works fine for me, the text is up against the left edge instead of being indented by 1 pixel because of the default Border. – camickr Jul 02 '10 at 16:46