当前位置: 首页 > 面试题库 >

Java combobox swing

隗星驰
2023-03-14
问题内容

我的表有两个字段:

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

我已经productName从上表冲进了一个组合框。

当用户Item从中的产品列表中选择时Ccombobox。我需要获取所选产品的相应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);
   }


 类似资料:

相关阅读

相关文章

相关问答