2013-10-11 62 views
0
class MyDocumentListener implements DocumentListener { 

    public void insertUpdate(DocumentEvent e) { 

     if(typeComboBox3.getSelectedIndex()==0) { 
         JOptionPane.showMessageDialog(null, "Please select an item type"); 
         serialnumberTextField3.setText(""); 
        } 

        else { 
        String sql2 = "INSERT INTO product(type, materialcode, serialnumber, status, careof, date, name, accountnumber, contactnumber) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)"; 
        try { 

        pst1 = conn.prepareStatement(sql2); 
        pst1.setString(1, (String)typeComboBox3.getSelectedItem()); 
        pst1.setString(2, (String)materialcodeComboBox3.getSelectedItem()); 
        pst1.setString(3, serialnumberTextField3.getText()); 
        pst1.setString(4, (String)statusComboBox3.getSelectedItem()); 
        pst1.setString(5, (String)careofComboBox3.getSelectedItem()); 
        pst1.setString(6, "2012-03-08"); 
        pst1.setString(7, ""); 
        pst1.setString(8, ""); 
        pst1.setString(9, ""); 
        pst1.execute(); 

        JOptionPane.showMessageDialog(null, "Type: "+typeComboBox3.getSelectedItem()+"\n"+"Material Code: "+materialcodeComboBox3.getSelectedItem()+"\n"+"Serial Number: "+serialnumberTextField3.getText()+"\n"+"Care of: "+careofComboBox3.getSelectedItem()+"\n\n"+"Product Successfully Added"); 

        serialnumberTextField3.setText(""); 
        //serialnumberTextField3.requestFocus(); 

        } 

        catch(Exception ex) { 
         System.out.println(ex); 
        } 

        UpdateTable(); 

        } 

    } 

    public void removeUpdate(DocumentEvent e) { 



    } 

    public void changedUpdate(DocumentEvent e) { 



    } 

} 

當一些粘貼在文本字段,按OK在確認數據有消息對話框後,我的程序自動插入數據到數據庫已成功插入我嘗試使用serialnumberTextField3.setText(「」)來清除TextField,因爲某些原因,TextField奇怪地不清除。我不能插入數據到我的數據庫後刪除我的文本字段的內容

回答

0

試試這個:

if(typeComboBox3.getSelectedIndex()==0) { 
new Thread(){ 
    @Override 
    public void run(){ 
     JOptionPane.showMessageDialog(null, "Please select an item type"); 
     serialnumberTextField3.setText(""); 
    } 
}.start(); 

}

+0

我說的是第二serialnumberTextField3.setText( 「」),這是其他條款內。 – ladyisfirst

+0

那麼你是否在其他塊中嘗試了上述代碼? – Arvind

相關問題