Reputation: 27
I'm trying to add row from one table to another, there's also datepicker and I want to check if dates match.
int i=jTable1.getSelectedRow();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
jXDatePicker1.setFormats(dateFormat);
String date = dateFormat.format(jXDatePicker1.getDate()).toString();
String c1=jTable1.getValueAt(i, 0).toString();
String c2=jTable1.getValueAt(i, 1).toString();
String c3=jTable1.getValueAt(i, 2).toString();
String c4=jTable1.getValueAt(i, 3).toString();
String c5=price.getText().toString();
String c6=jTextField1.getText().toString();
String c7=date;
model.addRow(new Object[]{c1, c2, c3, c4, c5, c6, c7});
jTable2.setModel(model);
while(jTable2.getRowCount()>1) {
for (int k = 0; k < jTable2.getRowCount(); k++) {
if (c7.equals(jTable2.getValueAt(k, 6).toString())) {
}
JOptionPane.showMessageDialog(null, "Record exist");
model.removeRow(jTable2.getRowCount()-1);
return;
}
}
It throws error even though the dates are different:
Nothing seems to work I tried to compare ids instead of dates
while(jTable2.getRowCount()>1) {
for (int k = 0; k < jTable2.getRowCount(); k++) {
int t1=Integer.parseInt(jTable2.getValueAt(k, 0).toString());
if (c1==t1) {
JOptionPane.showMessageDialog(null, "Record exist");
model.removeRow(jTable2.getRowCount()-1);
return;
}
}
}
parsed both values as int still nothing. My loop doesn't work
Upvotes: 0
Views: 160
Reputation: 32145
I think your if statement does nothing as mentioned.
Otherwise, instead of comparing the two dates as string, make them Date then compare them.
Just change those lines:
Date date = dateFormat.parse(jXDatePicker1.getDate().toString());
//your code
Date c7=date;
And then your if statement whould be:
Date rowDate= dateFormat.parse(jTable2.getValueAt(k, 6).toString());
if (c7.compareTo(rowDate)==0) { //the two dates are equal
JOptionPane.showMessageDialog(null, "Record exist");
model.removeRow(jTable2.getRowCount()-1);
return;
}
Upvotes: 1