- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to display only vertical grid lines in a JTable
At first, set the setShowGrid() to FALSE to disable all the grid lines. To display only vertical grid lines in a table, set the method setShowVerticalLines() to TRUE. Let us first create a table with rows and columns −
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, display only vertical grid lines −
table.setShowGrid(false); table.setShowVerticalLines(true);
The following is an example to display only vertical grid lines in a JTable −
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.setGridColor(Color.blue); table.setRowSelectionAllowed(true); table.setShowGrid(false); table.setShowVerticalLines(true); panel.add(new JScrollPane(table)); frame.add(panel); frame.setSize(550, 400); frame.setVisible(true); } }
Output
- Related Articles
- Java Program to display only horizontal grid lines in a JTable
- Java Program to display both horizontal and vertical grid lines in a JTable
- How to display horizontal grid lines in a JTable with Java?
- How to display vertical grid lines in a table with Java?
- How to prevent displaying any grid lines in a JTable?
- Java Program to select a column in JTable?
- Java Program to set the height of only a single row in a JTable with multiple rows
- Java Program to append a row to a JTable in Java Swing
- How to display "No records available" text in a JTable in Java? \n
- Java Program to deselect all cells in a JTable
- Java Program to enable cell selection in a JTable
- Java Program to enable column selection in a JTable
- Java Program to increase the row height in a JTable
- Disable only the vertical scrollbar in Java
- Java Program to deselect a range of columns in a JTable

Advertisements