2014-11-27 7 views
0

한 테이블에서 다른 테이블로 행을 추가하려고하는데 datepicker도 있고 날짜가 일치하는지 확인하고 싶습니다.테이블 행 비교, 중복 방지

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; 
    } 
} 

그것은 날짜가 다른 경우에도 오류가 발생합니다 :

enter image description here

아무것도 내가

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; 
       } 
      } 
     } 

가 int로서 두 값을 구문 분석하는 대신 날짜의 ID를 비교하는 시도 일 것 같다 아직 아무것도. 루프가 작동하지 않습니다.

+3

나는 당신의 들여 쓰기를 고정. if 문이 아무 것도 수행하지 않는다는 것이 분명해졌습니다. – mavroprovato

+0

변경된 스틸 그다지 일 동안 (jTable2.getRowCount()> 1) {(INT에서 K = 0; K user3316744

답변

1

if 문은 언급 한대로 작동하지 않습니다.

그렇지 않으면 문자열로 두 날짜를 비교하는 대신 날짜를 만들어 비교하십시오.

Date date = dateFormat.parse(jXDatePicker1.getDate().toString()); 
//your code 
Date c7=date; 

을 그리고 문이 될 whould 경우 다음 :

그냥 그 라인을 변경

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; 
    } 
+0

내 게시물을 편집 했으므로 작동하지 않습니다. 문이 실행되지 않지만 왜 그런지 모르겠습니다. – user3316744

+0

@ user3316744 만약 당신이 '내'대답처럼'안에'행동하기 전에 그 안에 어떤 코드도 없다. –