How to prevent displaying any grid lines in a JTable?

Java 8Object Oriented ProgrammingProgramming

Let’s say the following is our 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);

Prevent displaying grid lines −

table.setShowGrid(false);

The following is an example to prevent displaying grid lines −

Example

package my;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
public class SwingDemo {
   public static void main(String[] args) {
      JFrame frame = new JFrame();
      JPanel panel = new JPanel();
      panel.setBorder(BorderFactory.createTitledBorder(
         BorderFactory.createEtchedBorder(), "ODI Rankings", TitledBorder.CENTER, TitledBorder.TOP));
      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.setShowHorizontalLines(true);
      table.setGridColor(Color.orange);
      table.setRowSelectionAllowed(true);
      table.setShowGrid(false);
      panel.add(new JScrollPane(table));
      frame.add(panel);
      frame.setSize(550, 400);
      frame.setVisible(true);
   }
}

Output

raja
Published on 06-Jun-2019 08:47:13
Advertisements