从组合框中删除项目

时间:2012-04-08 17:31:23

标签: java swing combobox

我有一个从数据库填充组合框的方法。它使用医生ID来填充它。问题是,当我将ID更改为另一个人而不是清除组合框然后再次填充它时,它只是添加到列表的末尾。

public void FillTimings() {
    theapptTime.removeAllItems();
    theapptTime.repaint();

    String strDate = theTdate.getText().trim();

    String strDoctor = (String) theTstaffname.getSelectedItem();
    System.out.println(strDoctor);

    try {
        db.dbConnect();
        String docId = db.getdociD(strDoctor);
        ResultSet rs = db.getTimings(strDate, docId);

        while (rs.next()) {
            theapptTime.addItem(rs.getString(1));
            rs.close();
        }
    } catch (SQLException e1) {
        System.out.println("fail");
        e1.printStackTrace();
    }
}

这就是代码。

有人可以帮助我。

2 个答案:

答案 0 :(得分:0)

您必须首先声明DefaultComboBoxModel

    DefaultComboBoxModel dt=new DefaultComboBoxModel();

将您的组合框模型设置为此模型

    theapptTime.setModel(dt);

接下来在您的代码中 将其更新为

    db.dbConnect();
    String docId = db.getdociD(strDoctor);
    ResultSet rs = db.getTimings(strDate, docId);
    dt.removeAllElements();
    while (rs.next()) {
        dt.addElement(rs.getString(1));
        rs.close();
    }
它会起作用!!欢呼声!!

答案 1 :(得分:0)

试试这个:

DefaultComboBoxModel dt = (DefaultComboBoxModel) theapptTime.getModel();
dt.addElement("item 1");
相关问题