JTable Empty cell validation for the filled rows
I am doing a project in java swing in which I have to use a JTable. By default jtable can validate the inputs (float,int) by turning into red when wrong input is given. But I want to validate the empty cell left in the table at the time of save button click.
Here is my code:
public boolean validCheck() {
if (jTable.getCellEditor() != null) {
jTable.getCellEditor().stopCellEditing();
}
for (int i = 0; i < jTable3.getRowCount(); i++) {
for (int j = 0; j < jTable3.getColumnCount(); j++) {
String val = jTable3.getValueAt(row, col).toString();
if (val.trim().length() == 0) {
return false;(joptionpane.showmessagedialog(null,"field empty");)
}
}
}
return true;
}
This code is checking all the rows in the table and displaying the field empty message. I need to check onl开发者_开发知识库y filled rows(rows which have data) not all the rows in the jtable.
Any suggestions would be helpful.
You need to change your logic to first look if a row had data at all. Then (and only then), take a second pass to validate that all columns in that row are filled.
精彩评论