8 Kasım 2016 Salı

Java'da ComboBox'ları Parametreye Göre Otomatik Seçtirme


Merhaba değerli takipçilerimiz bugün Java'da birbirine bağlı bir şekilde seçtiğimizde alt Combo'ları açılan ComboBox'a değineceğiz.

1. Adım Domain 

ProductDomain.java;  

package tr.com.ny.dm;

public class ProductDomain {

       int productId;
       int categoryId;
       double price;
       String productName;
       String productDescription;
       String categoryName;

       public int getProductId() {
             return productId;
       }

       public void setProductId(int productId) {
             this.productId = productId;
       }

       public int getCategoryId() {
             return categoryId;
       }

       public void setCategoryId(int categoryId) {
             this.categoryId = categoryId;
       }

       public double getPrice() {
             return price;
       }

       public void setPrice(double price) {
             this.price = price;
       }

       public String getProductName() {
             return productName;
       }

       public void setProductName(String productName) {
             this.productName = productName;
       }

       public String getProductDescription() {
             return productDescription;
       }

       public void setProductDescription(String productDescription) {
             this.productDescription = productDescription;
       }

       public String getCategoryName() {
             return categoryName;
       }

       public void setCategoryName(String categoryName) {
             this.categoryName = categoryName;
       }

       public String toString() {

             return productName;

       }

       public Object[] getData() {

             Object[] data = { productId, productName, categoryName, productDescription };

             return data;

       }

}

1. Adım Veritabanı ve Arayüz
ProductDao.java

1. Method; Normal Listeleme; 
public static List<ProductDomain> selectProduct() {

             List<ProductDomain> list = new ArrayList<ProductDomain>();

             Connection conn = ConnectionDao.getConnnection();

             try {

                    ProductDomain selectCategory = null;
                    Statement query = conn.createStatement();

                    ResultSet rs = query.executeQuery("SELECT product.productId,product.productName, category.categoryName, product.price, product.price,product.productDescription "
                                               + "FROM product " + "LEFT JOIN Category ON product.categoryId=category.categoryId");

                    while (rs.next()) {
                           selectCategory = new ProductDomain();

                           selectCategory.setProductId(rs.getInt("product.productId"));
                           selectCategory.setProductName(rs.getString("product.productName"));
                           selectCategory.setCategoryName(rs.getString("category.categoryName"));
                           selectCategory.setPrice(rs.getDouble("product.price"));
                           selectCategory.setProductDescription(rs.getString("product.productDescription"));

                           list.add(selectCategory);

                    }

                    query.close();
                    conn.close();
             } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
             }

             return list;

       }



1. Metot Arayüz'de ComboBox'ın içinde çağırma; 

productNameBox.addItemListener(new ItemListener() {

                    @Override
                    public void itemStateChanged(ItemEvent arg0) {
                           productPriceBox.removeAllItems();
                           productCategoryBox.removeAllItems();
                           ProductDomain list = (ProductDomain) productNameBox.getSelectedItem();
                          
                           productPriceBox.addItem(list.getPrice());
                           productCategoryBox.addItem(list.getCategoryName());

                    }

             });

productNameBox ComboBox'ına ItemListener uygulandıktan sonra önce işlem yapmak comboBox'ları removeAllItems ile temizliyoruz ardından. Parametresini almak istediğimiz ComboBox'ı yani prodoductNameBox'ı cast ediyoruz. Ardından  Domain aracılığı ile çekmiş olduğumuz verileri comboBox'lara get ediyoruz....

Bugün ki dersimiz bu kadar bir sonra ki derste görüşmek dileğiyle.



Hiç yorum yok:

Yorum Gönder