如何在更新后自动刷新 jComboBox 数据?

How to auto refresh jComboBox data after update?

我有一个 jComboBox 从 MySQL 服务器数据库获取数据。

当我向数据库添加新数据时,jComboBox 没有显示,我必须重新打开我的程序才能将新数据添加到 jComboBox

如何自动刷新 jComboBox 数据?

这是我的代码:

private void dataComboBox(){
    try {
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop","root","");
        Statement stat = con.createStatement();
        String sql = "select id from perfume order by id asc";      
        ResultSet res = stat.executeQuery(sql);                             
        while(res.next()){
            Object[] ob = new Object[3];
            ob[0] = res.getString(1);
            jComboBox5.addItem(ob[0]);                                     
        }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
    }
}


private void showCBdata(){
    try {
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop","root","");
        Statement stat = con.createStatement();
        String sql = "select name from perfume where id='"+jComboBox5.getSelectedItem()+"'";  
        ResultSet res = stat.executeQuery(sql);

    while(res.next()){
        Object[] ob = new Object[3];
        ob[0]=  res.getString(1);            
        jTextField8.setText((String) ob[0]);
    }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
}


//call method
private void jComboBox5ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    showCBdata();
}

你能帮帮我吗?

谢谢..

你可以这样做它会自动刷新 combobox

try {
            comboBox.removeAllItems();

            sql = "SELECT * FROM `table_name`";
            rs = stmnt.executeQuery(sql);

        while (rs.next()) {
            String val = rs.getString("column_name");
            comboBox.addItem(val);
        }
    } catch (SQLException ex) {
        Logger.getLogger(DefineCustomer.class.getName()).log(Level.SEVERE, null, ex);
    }

removeAllItems(); 方法将清除组合框以确保不重复值。
您不需要创建单独的 Object 来添加 jComboBox,您也可以添加 String

Inzimam Tariq IT 的代码(以上):

try {
            comboBox.removeAllItems();

            sql = "SELECT * FROM `table_name`";
            rs = stmnt.executeQuery(sql);

        while (rs.next()) {
            String val = rs.getString("column_name");
            comboBox.addItem(val);
        }
    } catch (SQLException ex) {
        Logger.getLogger(DefineCustomer.class.getName()).log(Level.SEVERE, null, ex);
    }

我建议将所有这些代码放在 ActionListener 中。这样每次鼠标在 comboBox 上输入时,上面的代码都会 运行。您应该执行以下操作:

 public void mouseEntered(MouseEvent e) {
       //the above code goes here
    }

我建议使用 mouseListenerhttps://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

但是如果你想看看其他 ActionListeners 你可以在这里看到它们: https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

在数据库中添加新注册表后,执行 removeAllItems comboBox.removeAllItems();并重新填充组合框, 我的例子:

jComboLicorerias.removeAllItems();

try {
        Conector = Conecta.getConexion();
        Statement St = Conector.createStatement();
        try (ResultSet Rs = St.executeQuery(Query)) {
            while (Rs.next()) {
                jComboLicorerias.addItem(Rs.getString("nombreLicoreria"));
            }
            St.close();
        }
    } catch (SQLException sqle) {
        JOptionPane.showMessageDialog(null, "Error en la consulta.");