Reputation: 1546
I've got this JComboBox where the items are the results of a query. What I'm trying to do here is to set a label and a value to each item. The problem is that when I want to get the value of the item selected in order to create a new object, I can't. I can get the label as you can see, using the proveedorCombo.getSelectedItem() method, but I don't need that, I need the ID, how can I get it to create the object "a"? I maneged to get it in the "for bucle" as you can see, I display de label and the value, but I can't take that variable to the create object instance below:
public class AgregarPlato extends JDialog {
private final JPanel contentPanel = new JPanel();
public static void main(String[] args) {
try {
AgregarPlato dialog = new AgregarPlato();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public AgregarPlato() {
setBounds(100, 100, 546, 459);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBackground(Color.WHITE);
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
Vector model = new Vector();
final JComboBox proveedorCombo = new JComboBox(model);
proveedorCombo.putClientProperty("JComboBox.isTableCellEditor",
Boolean.TRUE);
proveedorCombo.setFont(new Font("Tahoma", Font.PLAIN, 11));
proveedorCombo.setBounds(91, 56, 168, 20);
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();
int value = Integer.parseInt(elementos[i][0].toString());
String label = elementos[i][1].toString();
model.addElement(new Item(value, label + " " + value));
}
JButton aceptarButton = new JButton("Aceptar");
aceptarButton.setBounds(332, 387, 86, 23);
contentPanel.add(aceptarButton);
aceptarButton.setFont(new Font("Tahoma", Font.PLAIN, 11));
aceptarButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
ArticuloDAO dao = new ArticuloDAO();
Articulo a = new Articulo();
System.out.println(proveedorCombo.getSelectedItem());
a.setProveedor(2);
dao.insert(a);
}
}
});
aceptarButton.setActionCommand("OK");
getRootPane().setDefaultButton(aceptarButton);
}
class ItemRenderer extends BasicComboBoxRenderer {
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected,
cellHasFocus);
if (value != null) {
Item item = (Item) value;
setText(item.getDescription().toUpperCase());
}
if (index == -1) {
Item item = (Item) value;
setText("" + item.getId());
}
return this;
}
}
class Item {
private int id;
private String description;
public Item(int id, String description) {
this.id = id;
this.description = description;
}
public int getId() {
return id;
}
public String getDescription() {
return description;
}
public String toString() {
return description;
}
}
public void actionPerformed(ActionEvent e) {
JComboBox proveedorCombo = (JComboBox) e.getSource();
Item item = (Item) proveedorCombo.getSelectedItem();
System.out.println(item.getId() + " : " + item.getDescription());
}
}
Upvotes: 0
Views: 2345
Reputation: 16987
You can use this code to get the ID:
Item item = (Item) proveedorCombo.getSelectedItem();
int id = item.getId();
By the way, you should use DefaultListModel
instead of Vector
to hold the list items. This is because if you modify the vector, the list will not change, but if you modify a DefaultListModel
, the changes appear in the list immediately.
Upvotes: 1