Java Swing-自定义JComboBox项

Java Swing-自定义JComboBox项,java,swing,Java,Swing,我在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()

我在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?

您会发现,最好从JComboBox的模型中添加/删除项,而不是直接从JComboBox中添加/删除项。因此,创建一个
DefaultComboxModel
对象,使其成为您的
JComboxModel
模型。然后将项目添加到模型中,您应该是金色的。例如:

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

// ......

comboModel.addItem(new ItemCombo(someProduct));
DefaultComboxModel ComboxModel=新的DefaultComboxModel();
JComboBox itemsComboBox=新的JComboBox(组合模型);//**固定的***
// ......
comboModel.addItem(newitemcombo(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 + "]";
    }
}
导入java.awt.Dimension;
导入javax.swing.*;
公共类TestCombo扩展了JPanel{
私有静态最终产品[]产品={
新产品(“一”,1.0),
新产品(“两个”,2.0),
新产品(“三”,3.0),
新产品(“四”,4.0),
新产品(“五”,5.0),
};
私有DefaultComboxModel ComboxModel=新的DefaultComboxModel();
私有JComboBox itemsComboBox=新JComboBox(组合模型);
公共测试组合(){
添加(itemsComboBox);
用于(产品:产品){
comboModel.addElement(新项目组合(产品));
}
itemsComboBox.addActionListener(e->{
ItemCombo ItemCombo=(ItemCombo)itemsComboBox.getSelectedItem();
System.out.println(“选择:+itemCombo.getProduct());
});
设置首选尺寸(新尺寸(400150));
}
私有静态void createAndShowGui(){
JFrame=newjframe(“TestCombo”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(新的TestCombo());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
公共静态void main(字符串[]args){
调用器(()->createAndShowGui());
}
}
类ItemCombo{
私人产品;
公共项目组合(产品p){
本产品=p;
}
@凌驾
公共字符串toString(){
返回product.getName();
}
公开双价{
return product.getPrice();
}   
公共产品{
退货产品;
}
}
类产品{
私有字符串名称;
私人双价;
公共产品(字符串名称,双倍价格){
this.name=名称;
这个价格=价格;
}
公共字符串getName(){
返回名称;
}
公开双价{
退货价格;
}
@凌驾
公共字符串toString(){
返回“产品[name=“+name+”,price=“+price+”];
}
}

Oops,答案中的代码已修复--需要将模型传递到组合构造函数库中!它确实帮了我很多忙@维托科斯塔:不客气。有关“概念证明”代码,请参见编辑。