Vitor Costa Vitor Costa - 24 days ago 17
Java Question

Swing - Custom JComboBox Item

I created a JComboBox inside a JFrame

JComboBox itemsComboBox = new JComboBox();


And then created a class

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();
}
}


and as far as goes my knowledge about combo boxes, now I was supposed to be able to do

itemsComboBox.addItem(new ItemCombo(Product));


but, it says it can't convert the ItemCombo object to a string. What am I doing wrong? Is there another way to create a custom JComboBox like this?

Answer

You'll find that it's better to add/remove items from the JComboBox's model rather than from the JComboBox directly. So create a DefaultComboBoxModel<ItemCombo> object, make it your JComboBox<ItemCombo>'s model. Then add items to the model, and you should be golden. For example:

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

// ......

comboModel.addItem(new ItemCombo(someProduct));

Proof of concept code:

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 + "]";
    }
}
Comments