我需要返回 JTable 的非空行数的函数。 getRowCount() 给出 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/