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) {
}
}
我的程序在TextField上粘贴了某些东西时自动将数据插入到数据库中,在确认数据已成功插入的消息对话框上按ok后我尝试使用serialnumberTextField3.setText(“”)清除TextField TextField奇怪的是由于某种原因不清楚。
答案 0 :(得分:0)
试试这个:
if(typeComboBox3.getSelectedIndex()==0) {
new Thread(){
@Override
public void run(){
JOptionPane.showMessageDialog(null, "Please select an item type");
serialnumberTextField3.setText("");
}
}.start();
}