Java组合摆动

我的表有两个字段:

 ProductID  (Primary Key)
 ProductName  (duplicate values will be present)

我已将productName刷新到上表中的Combobox中.

当用户从Ccombobox中的产品列表中选择Item时.我需要获取所选产品的相应ID.

try {
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/kart","root","");
    PreparedStatement statement=connection.prepareStatement("SELECT product_name,product_id from allproducts");
    ResultSet result = statement.executeQuery();

    while(result.next()){
        combo.add(result.getString(1));
    } 
} catch (SQLException ec) {
    System.out.println("Connection Failed! Check output console");
    ec.printStackTrace();
    return;
}

解决方法:

你的问题有点不完整,但我的猜测是你的JComboBox填充了String.如果是这样,您可能更好地使用将ProductID与ProductName相结合的自定义类的对象填充JComboBox(或更好的模型).要让组合框显示名称,您需要为您的类提供一个返回名称的toString()方法,或者为您的组合框提供一个显示名称的单元格渲染器.

编辑
例如,创建一个类MyComboItem,为它提供两个从数据库中填充的String字段,给它一个显示产品名称的toString()方法,并用这种类型的项填充你的JComboBox:

class MyComboItem {
   private String productId;
   private String productName;

   public MyComboItem(String productId, String productName) {
      this.productId = productId;
      this.productName = productName;
   }

   public String getProductId() {
      return productId;
   }

   public String getProductName() {
      return productName;
   }

   @Override
   public String toString() {
      return productName;
   }
}

编辑2

哪个可以这样使用:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

public class ComboItemTest {
   public static void main(String[] args) {
      DefaultComboBoxModel<MyComboItem> comboModel = 
              new DefaultComboBoxModel<MyComboItem>();

      // note that here you would fill the model with data from your database ***
      comboModel.addElement(new MyComboItem("x1234A", "Product 1"));
      comboModel.addElement(new MyComboItem("x1235A", "Product 2"));
      comboModel.addElement(new MyComboItem("x1236A", "Product 3"));
      comboModel.addElement(new MyComboItem("x1237A", "Product 4"));
      comboModel.addElement(new MyComboItem("x1238A", "Product 5"));
      comboModel.addElement(new MyComboItem("x1239A", "Product 6"));

      final JComboBox<MyComboItem> combobox = new JComboBox<MyComboItem>(comboModel);

      combobox.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            MyComboItem item = (MyComboItem) combobox.getSelectedItem();
            if (item != null) {
               System.out.printf("You've selected Product Name: %s, Product ID: %s%n", 
                     item.getProductName(), item.getProductId());
            }
         }
      });

      JOptionPane.showMessageDialog(null, new JScrollPane(combobox));

   }
}

编辑3
在您的情况下,您将使用ResultSet中的信息填充模型.也许是这样的:

   ResultSet result = statement.executeQuery();

   while(result.next()){
       String productName = result.getString(1);
       String productId = result.getString(2); // ???? not sure if this is valid
       MyComboItem comboItem = new MyComboItem(productId, productName);
       comboModel.addElement(comboItem);
   }
上一篇:plsql查找不到带中文的纪录


下一篇:javascript – ExtJS 4组合框在表单绑定上显示valueField而不是displayField