2013-01-08 1 views
0

사용자에게 메시지를 준거하려고합니다. 작성한 코드가 실행되어야한다고 확신하지만 jconformation 상자를 거부하면 코드가 실행됩니다. 비록 내가 취소하거나 아니요 코드가 여전히 실행되고 데이터가 커밋되면 제 경우에도 실행되지 않습니다. 당신이 showConfirmDialog로에 면밀한 관찰을했을 경우 내가이 내 코드를 중지하기 위해 무엇을 할 수는취소 옵션을 사용하여 jconformation 대화 상자를 닫은 후 코드 실행을 중지하십시오.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
     if(evt.getSource()==jButton1){ 
       JOptionPane.showConfirmDialog(rootPane, "Are You Sure You Want to Submit This Data and Genertate a New Remittance Id!"); 

       DBUtil util = new DBUtil(); 
     try { 

      Connection con = util.getConnection(); 
      PreparedStatement stmt = con.prepareStatement("INSERT INTO dbo.bk_det(rm_id,bk_name,bk_branch,bk_add,bk_ref,ref_dt) VALUES (?,?,?,?,?,?)"); 
      String rm = (tf_rm_id.getText().trim() == null || tf_rm_id.getText().equals("")) ? "0" : tf_rm_id.getText(); 
      Object bkn = (cb_bkname.getSelectedItem() == null || cb_bkname.getSelectedItem().equals("")) ? "NILL" : cb_bkname.getSelectedItem(); 
      Object bkbn = (cb_brname.getSelectedItem() == null || cb_brname.getSelectedItem().equals("")) ? "NILL" : cb_brname.getSelectedItem(); 
      Object bkpln = (cb_plname.getSelectedItem() == null || cb_plname.getSelectedItem().equals("")) ? "NILL" : cb_plname.getSelectedItem(); 
      String rf = (tb_bkref.getText().trim() == null || tb_bkref.getText().equals("")) ? "NILL" : tb_bkref.getText(); 
      String rfdt = (tf_refdt.getText().trim() == null || tf_refdt.getText().equals("")) ? "NILL" : tf_refdt.getText(); 
      stmt.setString(1, ""+(rm)); 
      stmt.setString(2, ""+(bkn)); 
      stmt.setString(3, ""+(bkbn)); 
      stmt.setString(4, ""+(bkpln)); 
      stmt.setString(5, ""+(rf)); 
      stmt.setString(6, ""+(rfdt)); 
      stmt.execute(); 

      PreparedStatement stmt2 = con.prepareStatement("INSERT INTO bk_rep(rm_id, br_name, br_desig, br_pf, dt_rep, mob) VALUES (?,?,?,?,?,?)"); 
      String rm1 = (tf_rm_id.getText().trim() == null || tf_rm_id.getText().equals("")) ? "0" : tf_rm_id.getText(); 
      String brn = (tfbrname.getText().trim() == null || tfbrname.getText().equals("")) ? "NILL" : tfbrname.getText(); 
      String brpf = (tf_brpf.getText().trim() == null || tf_brpf.getText().equals("")) ? "NILL" : tf_brpf.getText(); 
      String brdes = (tf_brdes.getText().trim() == null || tf_brdes.getText().equals("")) ? "NILL" : tf_brdes.getText(); 
      String brdtrep = (tf_rm_dt.getText().trim() == null || tf_rm_dt.getText().equals("")) ? "NILL" : tf_rm_dt.getText(); 
      String brmob = (tf_brmob.getText().trim() == null || tf_brmob.getText().equals("")) ? "NILL" : tf_brmob.getText(); 
      stmt2.setString(1, ""+(rm1)); 
      stmt2.setString(2, ""+(brn)); 
      stmt2.setString(3, ""+(brdes)); 
      stmt2.setString(4, ""+(brpf)); 
      stmt2.setString(5, ""+(brdtrep)); 
      stmt2.setString(6, ""+(brmob)); 

      stmt2.execute(); 

      PreparedStatement stmt3 = con.prepareStatement("INSERT INTO bk_sec([bs_name],[bs_desig],[rm_id],[dt_rep]) VALUES (?,?,?,?)"); 
      String rm2 = (tf_rm_id.getText().trim() == null || tf_rm_id.getText().equals("")) ? "0" : tf_rm_id.getText(); 
      String sn = (tfsname.getText().trim() == null || tfsname.getText().equals("")) ? "NILL" : tfsname.getText(); 
      String sdes = (tf_sdes.getText().trim() == null || tf_sdes.getText().equals("")) ? "NILL" : tf_sdes.getText(); 
      String bsdtrep = (tf_rm_dt.getText().trim() == null || tf_rm_dt.getText().equals("")) ? "NILL" : tf_rm_dt.getText(); 



      stmt3.setString(1, ""+(sn)); 
      stmt3.setString(2, ""+(sdes)); 
      stmt3.setString(3, ""+(rm2)); 
      stmt3.setString(4, ""+(bsdtrep)); 


      stmt3.execute(); 
      JOptionPane.showMessageDialog(null, "COMMITED SUCCESSFULLY!"); 
     } catch (Exception ex) { 
      JOptionPane.showMessageDialog(null, ex.getMessage()); 

     } 
     CalendarUtil cal=new CalendarUtil(); 
     tf_rm_id.setText(cal.getRemId()); 
     String datePrefix = new SimpleDateFormat("MMMM dd, YYYY").format(new Date()); 
     tf_rm_dt.setText(datePrefix); 
     tb_bkref.setText(""); 
     tf_refdt.setText(""); 
     tf_brpf.setText(""); 
     tf_brdes.setText(""); 
     tf_brmob.setText(""); 
     tfsname.setText(""); 
     tf_sdes.setText(""); 
     tfbrname.setText(""); 
     tf_scity.setText(""); 

     } 
    }  

답변

1

... 아래와 같습니다, 당신은 정수를 반환 보았을 것입니다. 나는 반환을 위해 API를 인용 할 것이다. 그것은 귀하의 경우 2를해야하므로

Returns: an integer indicating the option selected by the user

반환이 버튼의 순서해야합니다, 당신은 확인 대화 상자에서 얻었다.

당신은 가질 필요가이 값을 확인 지금 문,

이처럼 할 수 있다면 :

내가 if 문에서 또는에서 실행 내 코드를 작성해야 다음
int buffer = JOptionPane.showConfirmDialog(rootPane, "Are You Sure You Want to Submit This Data and Genertate a New Remittance Id!") 

// user pressed yes 
if(buffer == 0) { 
    try { 
     // whatever 
    } catch (Exception ex) { 
     JOptionPane.showMessageDialog(null, ex.getMessage()); 
    } 
} 
else if(buffer == 1) { 
    // user input = no 
} 

else if(buffer == 2) { 
    // user input = cancel 
} 
+0

else 부분 ?? –

+0

당신은 당신의 확인 대화 상자에 대해 다음 정수를 가지고 : 내가 나중에 덕분에 지금 내가이 형제 고맙습니다 남자를 지워 가지고 – SomeJavaGuy

+0

당신을 위해 내 대답을 편집합니다'0 = yes' '1 = no' '2 = cancel' : -) –

관련 문제