2016-11-08 5 views
1

私はJFrameのスイング - カスタムJComboBoxの項目

JComboBox itemsComboBox = new JComboBox(); 

内のJComboBoxを作成し、

public class ItemCombo { 

    Product p; 

    public ItemCombo(Product p) { 
     this.p = p; 
    } 

    @Override 
    public String toString(){ 
     return p.getName(); 
    } 

    public Float getPrice() { 
     return p.getPrice(); 
    }  
} 

と限りコンボボックスについての私の知識を行くように、今私はになっていたクラスを作成しましたできることは

itemsComboBox.addItem(new ItemCombo(Product)); 

ですが、ItemComboオブジェクトを文字列に変換することはできません。私は間違って何をしていますか?このようなカスタムJComboBoxを作成する別の方法はありますか?

+0

おっと、答えのコードは、コンボコンストラクタに –

答えて

2

JComboBoxから直接ではなく、JComboBoxのモデルから項目を追加/削除するほうがよいことがわかります。したがって、DefaultComboBoxModel<ItemCombo>オブジェクトを作成し、JComboBox<ItemCombo>のモデルにします。その後、モデルにアイテムを追加すると、あなたは金色になるはずです。たとえば:コンセプトコードの

DefaultComboBoxModel<ItemCombo> comboModel = new DefaultComboBoxModel<>(); 
JComboBox<ItemCombo> itemsComboBox = new JComboBox<>(comboModel); // *** fixed *** 

// ...... 

comboModel.addItem(new ItemCombo(someProduct)); 

証明:

import java.awt.Dimension; 
import javax.swing.*; 

public class TestCombo extends JPanel { 
    private static final Product[] products = { 
      new Product("One", 1.0), 
      new Product("Two", 2.0), 
      new Product("Three", 3.0), 
      new Product("Four", 4.0), 
      new Product("Five", 5.0), 
    }; 

    private DefaultComboBoxModel<ItemCombo> comboModel = new DefaultComboBoxModel<>(); 
    private JComboBox<ItemCombo> itemsComboBox = new JComboBox<>(comboModel); 

    public TestCombo() { 
     add(itemsComboBox); 
     for (Product product : products) { 
      comboModel.addElement(new ItemCombo(product)); 
     } 
     itemsComboBox.addActionListener(e -> { 
      ItemCombo itemCombo = (ItemCombo) itemsComboBox.getSelectedItem(); 
      System.out.println("Selection: " + itemCombo.getProduct()); 
     }); 

     setPreferredSize(new Dimension(400, 150)); 
    } 

    private static void createAndShowGui() { 
     JFrame frame = new JFrame("TestCombo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(new TestCombo()); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> createAndShowGui()); 
    } 
} 


class ItemCombo { 

    private Product product; 

    public ItemCombo(Product p) { 
     this.product = p; 
    } 

    @Override 
    public String toString(){ 
     return product.getName(); 
    } 

    public double getPrice() { 
     return product.getPrice(); 
    } 

    public Product getProduct() { 
     return product; 
    } 
} 

class Product { 

    private String name; 
    private double price; 

    public Product(String name, double price) { 
     this.name = name; 
     this.price = price; 
    } 

    public String getName() { 
     return name; 
    } 

    public double getPrice() { 
     return price; 
    } 

    @Override 
    public String toString() { 
     return "Product [name=" + name + ", price=" + price + "]"; 
    } 
} 
+0

感謝をモデルに合格する必要がありますfixed--!それは私を助けてくれました! –

+0

@VitorCosta:どうぞよろしくお願いいたします。 "proof of concept"コードの編集をご覧ください。 –

関連する問題