So I'm using this for loop to go through the values held in the first column. Then the if statement deletes the row if the value entered by the user is equal to the the value held in String hold. I am able to delete the first row but can not delete any row after that. What am I doing wrong?
private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
//adds a row to table
String name = JOptionPane.showInputDialog("Enter");
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
model.addRow(new Object[]{name, "JohnSMith@example.com", "03-16/05-17"});
}
private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {
// deletes row from table
String dName = JOptionPane.showInputDialog("Enter");
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
int rows = jTable1.getRowCount();
for(int i = 0; i <= rows;i++){
String hold = jTable1.getValueAt(i, 0).toString();
if(hold.equals(dName)){
model.removeRow(i);
}
break;
}
}
Aucun commentaire:
Enregistrer un commentaire