Java Swing How to - Config the grid line in JTable








Question

We would like to know how to config the grid line in JTable.

Answer

//www  . j av  a 2 s.  c  o  m

 

import java.awt.Color;

import javax.swing.JTable;

public class Main {
  public static void main(String[] argv) throws Exception {

    JTable table = new JTable();

    table.setShowGrid(false);//Don't show any grid lines

    //Show only vertical grid lines
    table.setShowGrid(false);
    table.setShowVerticalLines(true);
  
    //Show only horizontal grid lines
 
    table.setShowGrid(false);
    table.setShowHorizontalLines(true);

    //Set the grid color

    table.setGridColor(Color.red);

    //Show both horizontal and vertical grid lines (the default)
    table.setShowGrid(true);
  }
}