pheromix
pheromix

Reputation: 19347

How to count the number of rows of a JTable?

There is a JTable. I want to know the number of rows of it. How to achieve that ?

Upvotes: 3

Views: 21034

Answers (2)

A. Cedano
A. Cedano

Reputation: 975

You can use the getRowCount() method:

Returns the number of rows that can be shown in the JTable, given unlimited space. If a RowSorter with a filter has been specified, the number of rows returned may differ from that of the underlying TableModel.

Here you have one example:

import javax.swing.JTable;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[][] cellData = { { "1-1", "1-2" }, { "2-1", "2-2" } };
    String[] columnNames = { "col1", "col2" };

    JTable table = new JTable(cellData, columnNames);

    int rows = table.getRowCount();
    int cols = table.getColumnCount();

    System.out.println(rows);
    System.out.println(cols);
  }
}

Upvotes: 9

Shinwar ismail
Shinwar ismail

Reputation: 307

 DefaultTableModel t=(DefaultTableModel)jTable1.getModel();
             int number_of_rows = t.getRowCount();

note : if empty rows number_of_rows=0

Upvotes: 1

Related Questions