gpt4 book ai didi

java - 获取 JComboBox 项目值

转载 作者:行者123 更新时间:2023-12-01 14:16:58 26 4
gpt4 key购买 nike

我有这个 JComboBox,其中的项目是查询的结果。我在这里想做的是为每个项目设置一个标签和一个值。问题是,当我想获取所选项目的值以创建新对象时,我不能。我可以使用 ProveedorCombo.getSelectedItem() 方法获取标签,如您所见,但我不需要它,我需要 ID,如何获取它来创建对象“a”?我设法将其放入“for bucle”中,如您所见,我显示了标签和值,但我无法将该变量带到下面的创建对象实例中:

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

最佳答案

您可以使用此代码获取 ID:

Item item = (Item) proveedorCombo.getSelectedItem();
int id = item.getId();

顺便说一句,您应该使用 DefaultListModel 而不是 Vector 来保存列表项。这是因为,如果您修改 vector ,列表不会更改,但如果您修改 DefaultListModel,更改会立即出现在列表中。

关于java - 获取 JComboBox 项目值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18047870/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com