如何计算JTable的行数?

phe*_*mix 3 swing jtable

有一个JTable。我想知道它的行数。如何做到这一点?

A. *_*ano 6

您可以使用以下getRowCount()方法:

JTable给定无限空间的情况下,返回可以在 中显示的行数。如果RowSorter指定了带过滤器的 ,则返回的行数可能与底层 的行数不同 TableModel

这里有一个例子:

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);
  }
}
Run Code Online (Sandbox Code Playgroud)