0

I have a JComboBox where the items are the results of a query. The combo shows all the categories names taken from a query, right? Ok, it works. Now I need to give each item a value, which would be the ID of the product.

This is what I've got so far:

    final JComboBox proveedorCombo = new JComboBox();

    contentPanel.add(proveedorCombo);

    ProveedorDAO dao = new ProveedorDAO();

    List<Proveedor> proveedor = dao.getAll();

    Object[][] elementos = new Object[proveedor.size()][2];

    for (int i = 0; i < proveedor.size(); i++) {
        Proveedor p = proveedor.get(i);
        elementos[i][0] = p.getId();
        elementos[i][1] = p.getNombre();
        proveedorCombo.addItem(elementos[i][1]);
    }

As you can see in the code, the "label" of each item is the name of it. Now, how can I set each item its ID so I can manipulate after?

4

1 回答 1

0

您需要使用值的 ID 作为 JComboBox 中的值,并使用自定义 ListCellRenderer 来查找 ID 值的名称。

您可以通过按 ID 构建名称 Map 并让渲染器返回值的名称来做到这一点。

于 2013-08-01T22:40:48.040 回答