
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
Increase Row Height in JTable
In the given article, we will learn to write a program in Java to increase the row height in a JTable. The example creates a table with different languages and their difficulty levels and then modifies the row height using the setRowHeight() method.
Steps to increase the row height in a JTable
Following are the steps to increase the row height in a JTable ?
- Import the necessary classes from javax.swing package.
- Initialize a DefaultTableModel and create a JTable with it.
- Add columns to represent languages and difficulty levels.
- Insert and append rows to the table.
- Increase the row height by 20 pixels using setRowHeight().
- Display the table inside a JFrame.
Java program to increase the row height in a JTable
The following is an example to increase the row height ?
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("Difficulty Level"); tableModel.insertRow(0, new Object[] { "CSS", "Easy" }); tableModel.insertRow(0, new Object[] { "HTML5", "Easy"}); tableModel.insertRow(0, new Object[] { "JavaScript", "Intermediate" }); tableModel.insertRow(0, new Object[] { "jQuery", "Intermediate" }); tableModel.insertRow(0, new Object[] { "AngularJS", "Difficult"}); // adding a new row tableModel.insertRow(tableModel.getRowCount(), new Object[] {"ExpressJS", "Intermediate" }); // appending a new row tableModel.addRow(new Object[] { "WordPress", "Easy" }); // set row height table.setRowHeight(table.getRowHeight() + 20); JFrame f = new JFrame(); f.setSize(550, 350); f.add(new JScrollPane(table)); f.setVisible(true); } }
Output
The output is as follows. Here we have set the row height to 20 ?
Now, let us change the row height to 5 and spot the difference ?
Code Explanation
This Java program creates a table with languages and their corresponding difficulty levels using DefaultTableModel and JTable. Rows and columns are set up, with languages as one column and difficulty levels as another. To adjust row height, the program uses the setRowHeight() method, which increments the row height by 20 pixels, enhancing the display. The table is then displayed in a window using a JFrame wrapped inside a JScrollPane. This makes it easy to visually spot the row height increase in the table.