Reputation: 41
I require the function that returns the number of non empty rows of a JTable. getRowCount() gives the total number of rows(empty+filled=) in Jtable which is not desired.
Upvotes: 2
Views: 750
Reputation: 2479
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. 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;
}
Upvotes: 2