繁体   English   中英

获取JComboBox项目值

[英]get JComboBox Item Value

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

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来保存列表项。 这是因为,如果修改向量,列表将不会更改,但是如果修改DefaultListModel ,则更改将立即显示在列表中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM