Java combobox swing

My table has two fields:

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

I dropped productNamein Combobox from the above table.

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

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;
}
+4
source share
1 answer

, , JComboBox String. , , , JComboBox (, , ) , ProductID ProductName. combobox, toString(), , .


Edit
, MyComboItem, , , 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);
   }
+3

Source: https://habr.com/ru/post/1547308/


All Articles