Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count the number of rows of a JTable?

Tags:

swing

jtable

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

like image 756
pheromix Avatar asked Dec 14 '25 21:12

pheromix


2 Answers

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);
  }
}
like image 87
A. Cedano Avatar answered Dec 16 '25 22:12

A. Cedano


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

note : if empty rows number_of_rows=0

like image 28
Shinwar ismail Avatar answered Dec 16 '25 22:12

Shinwar ismail



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!