
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to get the number of rows and columns of a JTable in Java Swing
To count the number of rows of a table, use the getRowCount() method −
table.getRowCount()
To count the number of columns of a table, use the getColumnCount() method −
table.getColumnCount()
The following is an example to get the number of rows and columns of a JTable −
Example
package my; 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("Views"); // prevent from resizing table.getTableHeader().setResizingAllowed(false); tableModel.addRow(new Object[] { "F#", "Yes", "No", "7890"}); tableModel.addRow(new Object[] { "Blockchain", "Yes", "No", "10600"}); tableModel.addRow(new Object[] { "SharePoint", "Yes", "Yes", "4900"}); tableModel.addRow(new Object[] { "AWS", "No", "Yes", "8900"}); tableModel.addRow(new Object[] { "Python", "Yes", "No", "6789"}); tableModel.addRow(new Object[] { "Scala", "Yes", "No", "3400"}); tableModel.addRow(new Object[] { "Swift", "No", "Yes", "9676"}); tableModel.addRow(new Object[] { "C#", "Yes", "Yes", "1300"}); tableModel.addRow(new Object[] { "NodeJS", "No", "Yes", "2350"}); tableModel.addRow(new Object[] { "MVC", "Yes", "No", "1500"}); tableModel.addRow(new Object[] { "ASP.NET", "Yes", "Yes", "3400"}); tableModel.addRow(new Object[] { "Java", "Yes", "No", "9686"}); tableModel.addRow(new Object[] { "jQuery", "Yes", "Yes", "4500"}); System.out.println("Table rows = "+table.getRowCount()); System.out.println("Table columns = "+table.getColumnCount()); Font font = new Font("Verdana", Font.PLAIN, 12); table.setFont(font); JFrame frame = new JFrame(); frame.setSize(600, 400); frame.add(new JScrollPane(table)); frame.setVisible(true); } }
This will produce the following output displaying the table with rows and columns −
Output
The count of rows and columns are displayed in the console as shown below −
- Related Questions & Answers
- How to get rows and columns of 2D array in Java?
- How to add a title to JTable in Java Swing?
- Move the first row to the end of the JTable in Java Swing
- How to add JTable to Panel in Java Swing?
- How to set the color to alternate rows of JTable in Java?
- How to get the maximum number of occupied rows and columns in a worksheet in Selenium with python?
- Java Program to append a row to a JTable in Java Swing
- How to set a certain number of rows and columns of a Tkinter grid?
- Java Program to change the background color of rows in a JTable
- Java Program to deselect a range of columns in a JTable
- How can we prevent the re-ordering columns of a JTable in Java?
- How to add a new row to JTable with insertRow() in Java Swing
- Program to find number of columns flips to get maximum number of equal Rows in Python?
- How to get the number of columns of a table using JDBC?
- How to apply adjustments to the next column of a JTable only, when the width of a column is changed in Java Swing?
Advertisements