How to display vertical grid lines in a table with Java?


To display vertical grid lines in a table, use the setShowVerticalLines() method. Let us first create a table −

String[][] rec = {
   { "1", "Steve", "AUS" },
   { "2", "Virat", "IND" },
   { "3", "Kane", "NZ" },
   { "4", "David", "AUS" },
   { "5", "Ben", "ENG" },
   { "6", "Eion", "ENG" },
};
String[] header = { "Rank", "Player", "Country" };
JTable table = new JTable(rec, header);

Now, let us display vertical grid lines −

table.setShowVerticalLines(true);

You can also give a color to these lines −

table.setGridColor(Color.blue);

The following is an example to display vertical grid lines in JTable −

Example

package my;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class SwingDemo {
   public static void main(String[] args) {
      JFrame frame = new JFrame();
      JPanel panel = new JPanel();
      String[][] rec = {
         { "1", "Steve", "AUS" },
         { "2", "Virat", "IND" },
         { "3", "Kane", "NZ" },
         { "4", "David", "AUS" },
         { "5", "Ben", "ENG" },
         { "6", "Eion", "ENG" },
      };
      String[] header = { "Rank", "Player", "Country" };
      JTable table = new JTable(rec, header);
      table.setShowGrid(false);
      table.setShowVerticalLines(true);
      table.setGridColor(Color.blue);
      panel.add(new JScrollPane(table));
      frame.add(panel);
      frame.setSize(550, 400);
      frame.setVisible(true);
   }
}

Output


Updated on: 30-Jul-2019

262 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements