获取JComboBox项值

时间:2013-08-04 21:43:29

标签: java swing label jcombobox

我有这个JComboBox,其中的项目是查询的结果。我在这里要做的是为每个项目设置标签和值。问题是,当我想获取所选项目的值以创建新对象时,我不能。我可以使用proveedorCombo.getSelectedItem()方法获得标签,但我不需要,我需要ID,如何才能创建对象“a”?正如你所看到的那样,我想把它变成“for bucle”,我显示de label和值,但是我不能把这个变量带到下面的创建对象实例:

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

1 个答案:

答案 0 :(得分:1)

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

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

顺便说一下,您应该使用DefaultListModel代替Vector来保存列表项。这是因为如果您修改向量,列表将不会更改,但如果您修改DefaultListModel,则更改会立即显示在列表中。