Reputation: 19347
There is a JTable
. I want to know the number of rows of it. How to achieve that ?
Upvotes: 3
Views: 21034
Reputation: 975
You can use the getRowCount()
method:
Returns the number of rows that can be shown in the
JTable
, given unlimited space. If aRowSorter
with a filter has been specified, the number of rows returned may differ from that of the underlyingTableModel
.
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
Reputation: 307
DefaultTableModel t=(DefaultTableModel)jTable1.getModel();
int number_of_rows = t.getRowCount();
note : if empty rows number_of_rows=0
Upvotes: 1