如何在java中添加数据库id作为组合框索引?

发布于 2024-12-23 03:37:45 字数 728 浏览 5 评论 0原文

我想从数据库 ID 添加组合框索引。

 public static void selectCompany(javax.swing.JComboBox cmbCategory ){
    cmbCategory.removeAllItems();
    String sql="SELECT * FROM company_details";
    try{
        Connection conn=dbConnection();
        PreparedStatement pstmt=conn.prepareStatement(sql);
        ResultSet rs=pstmt.executeQuery(sql);
        while(rs.next()){
            int id=rs.getInt("company_id");
            String category=rs.getString("company_name");
            cmbCategory.addItem(id);
            cmbCategory.addItem(category);
        }
    }catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
}

cmbCategory 是组合框对象。我想将 id 显示为组合框索引,并将组合框列表显示为类别名称。数据库是mysql。

I want to add combobox Index from database id.

 public static void selectCompany(javax.swing.JComboBox cmbCategory ){
    cmbCategory.removeAllItems();
    String sql="SELECT * FROM company_details";
    try{
        Connection conn=dbConnection();
        PreparedStatement pstmt=conn.prepareStatement(sql);
        ResultSet rs=pstmt.executeQuery(sql);
        while(rs.next()){
            int id=rs.getInt("company_id");
            String category=rs.getString("company_name");
            cmbCategory.addItem(id);
            cmbCategory.addItem(category);
        }
    }catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
}

cmbCategory is combobox object. I want to dispaly id as combobox Index and Combobox list display as category name. Database is mysql.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野却迷人 2024-12-30 03:37:45

您可以将对象添加到 ComboBox,而不仅仅是字符串,因此类似这样的操作应该可以解决问题:

    while(rs.next()){
        int id=rs.getInt("company_id");
        String category=rs.getString("company_name");
        Object[] itemData = new Object[] {id, category};
        cmbCategory.addItem(itemData);
    }

正如 Harry Joy 指出的那样,您可以通过使用 ListCellRenderer

class MyListRenderer extends JLabel implements ListCellRenderer {

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        Object[] itemData = (Object[])value;
        setText((String)itemData[1]);

        return this;
    }
}

您可以稍后将其分配给 JComboBox:

cmbCategory.setRenderer(new MyListRenderer());

通过这样做,您可以得到具有明显的优势ID 和类别名称都在一个对象中,因此当用户在组合框中选择一项时,您可以访问该对象的所有属性(ID 和名称!)。

You can add objects to the ComboBox, not just strings, so something like this should do the trick:

    while(rs.next()){
        int id=rs.getInt("company_id");
        String category=rs.getString("company_name");
        Object[] itemData = new Object[] {id, category};
        cmbCategory.addItem(itemData);
    }

And as Harry Joy pointed out, you can tell swing how this element should be rendered by using a ListCellRenderer:

class MyListRenderer extends JLabel implements ListCellRenderer {

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        Object[] itemData = (Object[])value;
        setText((String)itemData[1]);

        return this;
    }
}

Which you can later assign to the JComboBox:

cmbCategory.setRenderer(new MyListRenderer());

By doing this, you have the obvious advantage of having both the ID and category name in just one object, so when the user selects an item in the combobox, you can access all the properties of this object (ID and name!).

转身泪倾城 2024-12-30 03:37:45

如果您想将 company_id 设置为 combobox 项目索引,那么我的答案是您无法设置项目索引。如果您想同时显示 ID 和类别,请连接 ID 和公司名称。

cmbCategory.addItem(id + " " + category);

If you want to set company_id as combobox item index then my answer is you can't set item index. If you want to display both, id and category then concatenate ID and company name.

cmbCategory.addItem(id + " " + category);
橙幽之幻 2024-12-30 03:37:45

当您使用自定义呈现器时,您将无法通过使用键盘输入项目的第一个字符来访问组合框项目。这是因为组合框模型的搜索使用每个项目的 toString() 方法来首先查找所请求的项目。在这种情况下,数组的 toString() 实现没有意义。

另一种解决方案是创建自定义对象并重写该对象的 toString() 方法,如下例所示: 如何使用 Map 元素作为 JComboBox 的文本

When you use a custom renderer you lose the ability to access the combobox items by using the keyboard to enter the first character of the item. This is because the search of the combobox model uses the toString() method of each item to first the requested item. The toString() implementation of an Array in not meaningul in this case.

A different solution is to create a custom object and override the toString() method of the object as shown in this example: How to use Map element as text of a JComboBox

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文