13

I'm trying to hide the grid lines of a JTable but with no results. Even trying to change the color of the grid lines does not work. Here is my code:

    // build the table
tableView = new JTable(ttm);
//Specifify the selection Listener and model
listSelectionModel = tableView.getSelectionModel();
listSelectionModel.addListSelectionListener(new SharedListSelectionHandler(tableView));
tableView.setSelectionModel(listSelectionModel);

//Add a mouse listener to our table and implement double click event
tableView.addMouseListener(new MouseAdapter(){

    public void mouseClicked(MouseEvent e){

        //If double click in a message show the Message Details window
        if (e.getClickCount() == 2){

            showMessageDetail();
            }
    }


} );

// set my own renderer
CustomCellRenderer mtr = new CustomCellRenderer();
tableView.setDefaultRenderer(Object.class, mtr);
// table properties
tableView.setGridColor(Color.black);
tableView.setShowGrid(false);
//tableView.setShowVerticalLines(false);
//tableView.setShowHorizontalLines(false);
tableView.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//hide header
tableView.setTableHeader(null);
// hide the id column
String columName = tableView.getColumnName(TableModel.COLUMN_ID);
tableView.getColumn(columName).setMaxWidth(0);
tableView.getColumn(columName).setMinWidth(0);
tableView.getColumn(columName).setWidth(0);
//load the messages in the table
loadMessages();
//adjust column width
tableView = autoResizeColWidth(tableView, ttm);


    public class CustomCellRenderer extends JPanel implements TableCellRenderer {
/**
 * First gradient color
 */
private static final Color COLOR_1 = new Color(255, 255, 255, 200);
/**
 * Second gradient color
 */
private static final Color COLOR_2 = new Color(255, 0, 255, 200);
/**
 * Controls gradient direction
 */
private static final float SIDE = 50;

private GradientPaint gradientPaint = new GradientPaint(0, 0, COLOR_1, SIDE, SIDE, COLOR_2, true);

private JLabel label = new JLabel();

    public CustomCellRenderer() {
        setOpaque(true);
        setLayout(new BorderLayout());
        add(label, BorderLayout.CENTER);
        label.setHorizontalAlignment(SwingConstants.CENTER);
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        label.setText(value.toString());
        return this;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setPaint(gradientPaint);
        g2.fillRect(0, 0, getWidth(), getHeight());
    }
    }

White grid lines are always being drawn. I'm stuck here...

Do I have to implement a custom Viewport to get rid of this?

Thanks in advance, Alex

AlejandroVK
  • 7,373
  • 13
  • 54
  • 77
  • 1
    That's weird. `JTable.setShowGrid(false)` works for me with a custom `TableCellRenderer`. Posting more relevant code may be beneficial, or better yet, post a [SSCCE](http://www.sscce.org). – Jeffrey Jan 25 '12 at 00:13
  • 1
    `I'm using a customized CellRenderer,dunno really if this might be causing the problem.` Well thats easy enough to figure out. Try your code without the custom renderer. – camickr Jan 25 '12 at 01:29
  • @camickr Tried that, didn't work out. – AlejandroVK Jan 25 '12 at 07:45
  • possible duplicate of [remove cells border in a jtable](http://stackoverflow.com/questions/3167112/remove-cells-border-in-a-jtable) – Suma Mar 24 '15 at 14:17

2 Answers2

36

you have to set two thingies

  • disable grid showing
  • zero row/column intercell spacing

In code:

table.setShowGrid(false);
table.setIntercellSpacing(new Dimension(0, 0));

Or use JXTable (from the SwingX project) which does it for you:

xTable.setShowGrid(false, false);
kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • 2
    Thanks man, this intercell spacing was driving me insane. I'd disable grid lines, and it looked like a white grid was there. Lost hours to this. – Adam Hughes Mar 23 '16 at 16:21
1

just jTable1.setShowHorizontalLines(false); or jTable1.setShowVerticalLines(false); or you can use the 2 option

jTable1.setShowHorizontalLines(false);

jTable1.setShowVerticalLines(false);