2016-11-08 36 views
1

我創建一個JComboBox,一個JFrame搖擺 - 自定義的JComboBox項目

JComboBox itemsComboBox = new 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

哎呀,在回答代碼fixed--需要模型傳遞到組合構造 –

回答

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

謝謝!它確實幫了我很多! –

+0

@VitorCosta:不客氣。請參閱編輯「概念驗證」代碼。 –