Java Swing How to - Get the Number of Rows and Columns in JTable








Question

We would like to know how to get the Number of Rows and Columns in JTable.

Answer

//from   w w  w. j av a  2  s  .  c o m

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);
  }
}