如何在组合框中设置所选项目并在文本字段中设置等效值

时间:2014-06-25 05:09:43

标签: java swing textfield jcombobox

我试图弄清楚如何设置我的代码,以便在组合框中选择一个选项时,它也会更改文本字段的文本。根据我的理解,它涉及一个叫做项目监听器的东西,但我们没有在课堂上讨论过这个问题。

另一个值与组合框的值相同,例如juan =' 123456'

我尝试了事件监听器,但仍然没有得到组合框的值

有人出来可以提前感谢

2 个答案:

答案 0 :(得分:4)

首先看看How to Use Combo Boxes,特别是Handling Events on a Combo Box,你也应该引用JavaDocs

例如

ComboBox

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

public class ComboBoxTest {

    public static void main(String[] args) {
        new ComboBoxTest();
    }

    private JComboBox<Product> cbProduts;
    private JTextField tfSku;

    public ComboBoxTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                DefaultComboBoxModel<Product> productModel = new DefaultComboBoxModel<>();
                productModel.addElement(new Product("Bananas", "1234"));
                productModel.addElement(new Product("Apples", "56789"));
                productModel.addElement(new Product("Pears", "00001"));
                productModel.addElement(new Product("Grapes", "00002"));

                cbProduts = new JComboBox<>(productModel);
                cbProduts.setRenderer(new ProductListCellRenderer());
                tfSku = new JTextField(5);

                cbProduts.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Product p = (Product) cbProduts.getSelectedItem();
                        if (p != null) {
                            tfSku.setText(p.getSKU());
                        } else {
                            tfSku.setText(null);
                        }
                    }
                });

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(cbProduts);
                frame.add(tfSku);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class Product {

        private final String description;
        private final String sku;

        public Product(String description, String sku) {
            this.description = description;
            this.sku = sku;
        }

        public String getDescription() {
            return description;
        }

        public String getSKU() {
            return sku;
        }

    }

    public static class ProductListCellRenderer extends DefaultListCellRenderer {

        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

            if (value instanceof Product) {
                value = ((Product)value).getDescription();

            }
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            return this;

        }

    }

}

答案 1 :(得分:0)

private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {                                            
    txt1.setText((String) jComboBox1.getSelectedItem());
}