- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to display horizontal grid lines in a JTable with Java?
To display horizontal grid lines in a table, use the setShowHorizontalLines() method and set it to TRUE. 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.setShowHorizontalLines(true);
You can also give a color to these lines −
table.setGridColor(Color.orange);
The following is an example to display horizontal grid lines in 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.setShowGrid(false); table.setShowHorizontalLines(true); table.setGridColor(Color.orange); panel.add(new JScrollPane(table)); frame.add(panel); frame.setSize(550, 400); frame.setVisible(true); } }
This will produce the following 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
- Java Program to display only vertical grid lines in a JTable
- How to display vertical grid lines in a table with Java?
- How to prevent displaying any grid lines in a JTable?
- Bootstrap Grid Stacked to horizontal grid with fluid container
- How to display "No records available" text in a JTable in Java?
- Bootstrap Grid Stacked to horizontal grid
- Disable auto resizing to make the JTable horizontal scrollable in Java
- How to enable row selection in a JTable with Java
- Display multiple lines of text in a component’s tooltip with Java
- Stacked-to-horizontal Bootstrap Grid
- How to select the first column in a JTable with Java?
- How to create horizontal lines with two different colors after a threshold in R?
- How to set a value in a particular JTable cell with Java?

Advertisements