【问题标题】:How to auto refresh jComboBox data after update?更新后如何自动刷新jComboBox数据?
【发布时间】:2016-07-11 03:52:34
【问题描述】:

我有一个 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();
}

你能帮帮我吗?

谢谢你..

【问题讨论】:

    标签: java mysql jcombobox


    【解决方案1】:

    你可以这样做它会自动刷新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

    【讨论】:

      【解决方案2】:

      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

      【讨论】:

        【解决方案3】:

        在数据库中添加新注册表后,请执行 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."); 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-14
          • 2016-05-03
          • 1970-01-01
          • 2017-06-10
          • 1970-01-01
          相关资源
          最近更新 更多