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) {



    }

}

当在 TextField 上粘贴某些内容时,我的程序会自动将数据插入数据库,在确认数据已成功插入的消息对话框上按确定后,我尝试使用 serialnumberTextField3.setText("") 清除 TextField,但 TextField 奇怪的是没有'由于某种原因不清楚。

4

1 回答 1

0

尝试这个:

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

}

于 2013-10-11T09:46:08.387 回答