简体   繁体   English

如何计算 JTable 中的非空行?

[英]How to count non empty rows in JTable?

I require the function that returns the number of non empty rows of a JTable.我需要返回 JTable 的非空行数的函数。 getRowCount() gives the total number of rows(empty+filled=) in Jtable which is not desired. getRowCount() 给出了 Jtable 中不需要的总行数(空+填充=)。

JTable

I have no experience with JTables, so I'm not sure if this is the 'best' solution, but a very simple way of doing this that i can think of would be iterating through each of the rows and checking if all the values in that row's columns are empty.我没有使用 JTables 的经验,所以我不确定这是否是“最佳”解决方案,但是我能想到的一个非常简单的方法是遍历每一行并检查所有值是否在该行的列是空的。 If all of the columns are null then increase the number of empty rows by one and return that value at the end of the function:如果所有列都为空,则将空行数增加一并在函数结束时返回该值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM