
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Set Height of a Single Row in JTable with Multiple Rows
In this article, we create a JTable in Java Swing to display information about different programming technologies and their tutorial availability. The table is set up with specific columns and rows, each containing data on a particular technology. We also demonstrate how to set a custom height for a single row in the table. Specifically, the height of the 4th row is adjusted to 30 pixels to show how you can control row height individually in a JTable. The table is then displayed within a scrollable JFrame window.
table.setRowHeight(3, 30);
The above sets row height to 30 pixels for row index 4.
Steps to set the height of only a single row in a JTable
Following are the steps to set the height of only a single row in a JTable ?
- First, we will import JFrame, JScrollPane, JTable, and DefaultTableModel for creating the UI and handling table data.
- Create an instance of DefaultTableModel to manage the data.
- Instantiate JTable with tableModel to apply this data model to the table.
- Add columns for "Language/Technology," "Text Tutorial," "Video Tutorial," and "Interview QA."
- Insert rows with data about different programming technologies and tutorial availability.
- Set the height of the row at an index of 3 to 30 pixels with the table.setRowHeight(3, 30).
- Place the table in a JScrollPane and add it to a JFrame window.
- Make the frame visible using f.setVisible(true).
Java program to set the height of only a single row in a JTable
The following is an example to set the height of a row in a table ?
package my; 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.setRowHeight(3, 30); JFrame f = new JFrame(); f.setSize(600, 400); f.add(new JScrollPane(table)); f.setVisible(true); } }
Output
The output is as follows. Here, we have set the row height for row 4 ?
Code explanation
In the above Java Swing program we have created a JTable to display programming technology data with custom row heights. Using DefaultTableModel, columns are defined and rows are added. We set the height of the 4th row to 30 pixels using the table.setRowHeight(3, 30). The table is added to a JFrame with a JScrollPane for scrolling and the frame is displayed using f.setVisible(true).