swing - 如何统计JTable的行数?

标签 swing jtable

有一个JTable。我想知道它的行数。如何实现?

最佳答案

您可以使用 getRowCount() 方法:

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你有一个例子:

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

关于swing - 如何统计JTable的行数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42177966/

相关文章:

java - 如何在不使用按钮的情况下从 JList 中选择项目?

java - 禁用-启用 jframe eclipse

java - JTable、JLabel 和它的图标(JLabel 不能用图标自定义)?

java - JXTable 中的自定义 header 渲染器?

java - 使用 Java Swing 从 sprite 表绘图的最佳性能选项

java - 如有必要,在 Java Swing EDT 中运行函数

java - Jtable 与 AbstractTableModel 删除所有行

java - jTable索引错误

java - 通过选中另一列中的一项来启用和禁用 JTable 复选框

java - 使用 setValueAt 重新创建互斥复选框