- 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
Java Program to set different height for multiple rows in JTable
To set different height for multiple rows, use the setRowHeight() method for separate rows for which you want to increase the row height. Let us first see an example to create a table with same height for all the rows −
Example
package my; import java.awt.Color; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class SwingDemo { public static void main(String[] argv) throws Exception { DefaultTableModel tableModel = new DefaultTableModel(); JTable table = new JTable(tableModel); tableModel.addColumn("Language/ Technology"); tableModel.addColumn("Text Tutorial"); tableModel.addColumn("Video Tutorial"); tableModel.addColumn("Interview QA"); tableModel.addRow(new Object[] { "Blockchain", "Yes", "No", "Yes"}); tableModel.addRow(new Object[] { "C#", "Yes", "Yes", "Yes"}); tableModel.addRow(new Object[] { "Java", "Yes", "No", "Yes"}); tableModel.addRow(new Object[] { "NodeJS", "No", "Yes", "Yes"}); tableModel.addRow(new Object[] { "MVC", "Yes", "No", "Yes"}); tableModel.addRow(new Object[] { "ASP.NET", "Yes", "Yes", "Yes"}); tableModel.addRow(new Object[] { "F#", "Yes", "No", "Yes"}); tableModel.addRow(new Object[] { "SharePoint", "Yes", "Yes", "Yes"}); tableModel.addRow(new Object[] { "AWS", "No", "No", "Yes"}); table.setBackground(Color.orange); table.setForeground(Color.black); Font font = new Font("Verdana", Font.CENTER_BASELINE, 12); table.setFont(font); JFrame frame = new JFrame(); frame.setSize(600, 400); frame.add(new JScrollPane(table)); frame.setVisible(true); } }
The output is as follows displaying same height for all the rows. This is the default for all the rows −
Output
Now, let us see an example to set different heights for multiple rows. Here, we are using the setRowHeight() method multiple times to give different height to rows −
Example
package my; import java.awt.Color; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class SwingDemo { public static void main(String[] argv) throws Exception { DefaultTableModel tableModel = new DefaultTableModel(); JTable table = new JTable(tableModel); tableModel.addColumn("Language/ Technology"); tableModel.addColumn("Text Tutorial"); tableModel.addColumn("Video Tutorial"); tableModel.addColumn("Interview QA"); tableModel.addRow(new Object[] { "Blockchain", "Yes", "No", "Yes"}); tableModel.addRow(new Object[] { "C#", "Yes", "Yes", "Yes"}); tableModel.addRow(new Object[] { "Java", "Yes", "No", "Yes"}); tableModel.addRow(new Object[] { "NodeJS", "No", "Yes", "Yes"}); tableModel.addRow(new Object[] { "MVC", "Yes", "No", "Yes"}); tableModel.addRow(new Object[] { "ASP.NET", "Yes", "Yes", "Yes"}); tableModel.addRow(new Object[] { "F#", "Yes", "No", "Yes"}); tableModel.addRow(new Object[] { "SharePoint", "Yes", "Yes", "Yes"}); tableModel.addRow(new Object[] { "AWS", "No", "No", "Yes"}); // set different height for different rows table.setRowHeight(2, 50); table.setRowHeight(4, 35); table.setRowHeight(6, 20); table.setRowHeight(8, 45); table.setBackground(Color.orange); table.setForeground(Color.black); Font font = new Font("Verdana", Font.CENTER_BASELINE, 12); table.setFont(font); JFrame frame = new JFrame(); frame.setSize(600, 400); frame.add(new JScrollPane(table)); frame.setVisible(true); } }
The output is as follows displaying rows with different heights −
Output
- Related Articles
- Java Program to set the height of only a single row in a JTable with multiple rows
- How to set the color to alternate rows of JTable in Java?
- Java Program to increase the row height in a JTable
- How to align multiple buttons with different height in Java?
- Java Program to change the background color of rows in a JTable
- Java Program to set date formats for different countries
- How to select different cells of a JTable programmatically in Java?
- Java Program to select a column in JTable?
- How to set multidimensional array into JTable with Java?
- Java Program to add Combo Box to JTable
- How to set magins between cells of a JTable in Java?
- Java Program to enable cell selection in a JTable
- Java Program to enable column selection in a JTable
- Java Program to deselect all cells in a JTable
- Java Program to append a row to a JTable in Java Swing

Advertisements