如何在java中添加数据库id作为组合框索引?
我想从数据库 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将对象添加到 ComboBox,而不仅仅是字符串,因此类似这样的操作应该可以解决问题:
正如 Harry Joy 指出的那样,您可以通过使用 ListCellRenderer:
您可以稍后将其分配给 JComboBox:
通过这样做,您可以得到具有明显的优势ID 和类别名称都在一个对象中,因此当用户在组合框中选择一项时,您可以访问该对象的所有属性(ID 和名称!)。
You can add objects to the ComboBox, not just strings, so something like this should do the trick:
And as Harry Joy pointed out, you can tell swing how this element should be rendered by using a ListCellRenderer:
Which you can later assign to the JComboBox:
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!).
如果您想将 company_id 设置为
combobox
项目索引,那么我的答案是您无法设置项目索引。如果您想同时显示 ID 和类别,请连接 ID 和公司名称。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.当您使用自定义呈现器时,您将无法通过使用键盘输入项目的第一个字符来访问组合框项目。这是因为组合框模型的搜索使用每个项目的 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