java - 如何计算JTable中的非空行数?

标签 java

我需要返回 JTable 的非空行数的函数。 getRowCount() 给出 Jtable 中不需要的总行数(空+填充=)。

JTable

最佳答案

我没有使用 JTables 的经验,所以我不确定这是否是“最佳”解决方案,但我能想到的一种非常简单的方法是迭代每一行并检查是否全部该行列中的值为空。如果所有列都为空,则将空行数加一并在函数末尾返回该值:

public int emptyRows(JTable table) {
    int emptyRows = 0;
    rowSearch: for (int row = 0; row < table.getRowCount(); row++) { //Iterate through all the rows
        for (int col = 0; col < table.getColumnCount(); col++) { //Iterate through all the columns in the row
            if (table.getValueAt(row, col) != null) { //Check if the box is empty
                continue rowSearch; //If the value is not null, the row contains stuff so go onto the next row
            }
        }
        emptyRows++; //Conditional never evaluated to true so none of the columns in the row contained anything
    }
    return emptyRows;
}

关于java - 如何计算JTable中的非空行数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33659081/

相关文章:

java - 使用矩阵参数创建 GET 请求

java - 如何将 double[] 转换为 double 作为我的返回值? (不兼容类型错误)

java - 如何在 JPQL 中找到第一个

java - 统计数据分析中的分散数据集

Java嵌套嵌套类

java - JSF onchange 事件

java - ResultSet.updateRow() 产生 "Illegal mix of collations (latin1_bin,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation ' <= >'"

java.util.Comparator.naturalOrder 采用 <T extends Comparable<? super T>> 并返回一个 Comparator<T> - 为什么?

Java 如何创建 UTC 并向同一个 UTC 对象添加几毫秒

java - 修改黑莓 OS5-7 BrowserField 中的后退按钮行为