Java 组合框摆动 [英] Java combobox swing

查看:19
本文介绍了Java 组合框摆动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表有两个字段:

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

我已将 productName 刷新到上表中的组合框.

I have flushed productName into a Combobox from the table above.

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

When a user selects an Itemfrom the list of products in the Ccombobox. I need to get the corresponding ID of the product selected.

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() 方法,或者为您的组合框提供一个显示名称的单元格渲染器.

Your question is somewhat incomplete, but my guess is that your JComboBox is populated with String. If so it would likely be better for you to populate the JComboBox (or better, its model) with objects of a custom class that combines your ProductID with a ProductName. To have the combobox display the name, you will need to either give your class a toString() method that returns the name, or give your combo box a cell renderer that shows the name.

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

Edit
For example, create a class, MyComboItem, give it two String fields that you fill from your database, give it a toString() method that shows the product name, and fill your JComboBox with items of this type:

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 中的信息填充您的模型.也许是这样的:


Edit 3
In your case, you'd fill your model with information from the ResultSet. Perhaps something like:

   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);
   }

这篇关于Java 组合框摆动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆