在 jComboBox 中显示数据

Posted

技术标签:

【中文标题】在 jComboBox 中显示数据【英文标题】:Display data in jComboBox 【发布时间】:2016-01-31 21:02:49 【问题描述】:

我正在尝试将数据从另一个类中的数据库传递到 jcombobox 中的 JFrame,但只显示最后一行。下面是一个示例代码:

A级...

public String jcbNames() 
    try 
        con = connCC.getDBconnection();
        stm = con.createStatement();
        ResultSet rs = stm.executeQuery("Select customerName From appointment");

        while (rs.next()) 
            customer = (rs.getString(1));
        
        con.close();

     catch (SQLException ex) 
        ex.printStackTrace();
     catch (ClassNotFoundException ex) 
        Logger.getLogger(Appointment.class.getName()).log(Level.SEVERE, null, ex);
    
    return customer;

类 JFrame...

private void jCBCustomersActionPerformed(java.awt.event.ActionEvent evt)                                              
    Appointment app = new Appointment();
    this.jCBCustomers.removeAllItems();
    this.jCBCustomers.addItem(app.jcbNames());

【问题讨论】:

您可能想看看How to Use Combo Boxes。但也请记住,您只返回来自jcbNames 的最后一个“客户” 【参考方案1】:

问题是您在 jcbNames 方法中只返回一个字符串,使用数组列表并将所有字符串添加到其中,然后将其作为数据库数据的集合返回。

所以改变你的方法或使用这个修改后的方法。

public ArrayList<String> jcbNames() 
    try 
        con = connCC.getDBconnection();
        stm = con.createStatement();
        ResultSet rs = stm.executeQuery("Select customerName From appointment");
        ArrayList<String> list = new ArrayList<>();
        while (rs.next()) 
            list.add(rs.getString(1));
        
        con.close();

     catch (SQLException ex) 
        ex.printStackTrace();
     catch (ClassNotFoundException ex) 
        Logger.getLogger(Appointment.class.getName()).log(Level.SEVERE, null, ex);
    
    return list;

private void jCBCustomersActionPerformed(java.awt.event.ActionEvent evt)                                              
    Appointment app = new Appointment();
    this.jCBCustomers.removeAllItems();
    ArrayList<String> list =app.jcbNames();
    for (String str:list)
         this.jCBCustomers.addItem(str);
    

如果您不想使用this.jCBCustomers.removeAllItems(); 来防止重复,请使用以下代码

private void jCBCustomersActionPerformed(java.awt.event.ActionEvent evt)       
    Appointment app = new Appointment();

    // collect all of your current data in jcombobox
    ArrayList<String> current_list = new ArrayList<>();
    int count = this.jCBCustomers.getItemCount(); // get count of them
    for (int i = 0; i < count; i++) 
        current_list.add((String) this.jCBCustomers.getItemAt(i)); // add them to current list
    

    // data that returned from database
    ArrayList<String> returned_from_db_list = jcbNames(); // calling jcbNames method

    /*
        for each string that has returned from database , 
        if it doesn't in the current_list 
        you can add it to jcombobox , so ...
    */ 
    for (String str : returned_from_db_list) 
        if ( !current_list.contains(str) )  // check for existing in the current_list
            current_list.add(str); // adding fresh data to current_list!
            /*
                or you can add them directly to jcombobox and remove above statement.
                this.jCBCustomers.addItem(str); // add updated data to jcombobox
            */
        
    

    /* 
        if   ::: you use   current_list.add(str); in above for-each-loop ,
               now you must update jcombobox data !
        else ::: remove below loop.
    */
    for (String singleStr : current_list) 
        this.jCBCustomers.addItem(singleStr); // add updated data to jcombobox
    

【讨论】:

谢谢,它有效,但我如何阻止 jcombobox 重复,因为如果我离开 this.jcb.removeAll () 它只选择第一个,如果我把它拿出来,它会创建重复。 @Meli,我更新了我的答案,检查它的最后一部分来做到这一点,

以上是关于在 jComboBox 中显示数据的主要内容,如果未能解决你的问题,请参考以下文章

在 JComboBox 中显示属性并注册另一个属性

JcomboBox 仅显示一项

让 JComboBox 在 JTable 中显示而不先单击它

浅析JTable与TableModelTableCellRendererTableCellEditor接口——使用JComboBox显示单元格的值

从数据库动态添加 jcombobox 中的值

从 JComboBox 弹出窗口中获取当前突出显示的项目(未选中的项目)