JComboBox从所选项目中的多个变量中获取变量

时间:2014-07-20 13:47:36

标签: java swing jcombobox

在我的代码中,我有一个JComboBox,我在其中放置了来自数据库的多个项目,所选的项目是用动作监听器打印出来的,但我想得到的是变量的值#34;代码&#34 ;从comboBox.addItem("Code: "+code+" Title: "+name);

中选择
final JComboBox comboBox = new JComboBox();
            comboBox.setBounds(139, 40, 337, 20);
            contentPanel.add(comboBox);
            Connection con;
            try {
                con = DriverManager.getConnection(Main.URL);
                Statement statement = con.createStatement();
                ResultSet resultSet = statement.executeQuery("select * from APPDATABASE.SUBJECT");
                while (resultSet.next()){
                    String code = resultSet.getString(1);
                    String name = resultSet.getString(2);
                    comboBox.addItem("Κωδ: "+code+"    Τίτλος: "+name);
                }
                ActionListener actionListener = new ActionListener() {
                  public void actionPerformed(ActionEvent actionEvent) {
                    System.out.println("Selected: " + comboBox.getSelectedItem());
                  }
                };
                comboBox.addActionListener(actionListener);

                resultSet.close();
                statement.close();
                con.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }

1 个答案:

答案 0 :(得分:1)

这是一个简单的示例代码。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import swingdemoapp.table.model.ComboBoxItem;

public class GetItemsFromJComboBoxPanel extends JPanel implements ActionListener {

    private final JComboBox<ComboBoxItem> items;
    private final DefaultComboBoxModel model;
    private final ComboBoxItem[] itemList = new ComboBoxItem[100];

    private final JLabel codeLabel;

    public GetItemsFromJComboBoxPanel() {

        JPanel jPanel = new JPanel(new BorderLayout());
        initList();
        model = new DefaultComboBoxModel(itemList);
        items = new JComboBox<>(model);
        items.addActionListener(this);
        jPanel.add(items, BorderLayout.NORTH);
        codeLabel = new JLabel();
        jPanel.add(codeLabel, BorderLayout.CENTER);
        this.add(jPanel);
    }

    private ComboBoxItem[] initList() {
        for(int i = 0; i < 100; i++) {
            itemList[i] = new ComboBoxItem("Code" + i, "My Item " + i);
        }

        return itemList;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource().equals(items)) {
            ComboBoxItem selectedItem = (ComboBoxItem) items.getModel().getSelectedItem();
            codeLabel.setText(selectedItem.getCode());
        }
    }


}

这是域类 ComboBoxItem

public class ComboBoxItem {

    private String code;
    private String title;

    public ComboBoxItem() {
    }

    public ComboBoxItem(String code, String title) {
        this.code = code;
        this.title = title;
    }

    /**
     * @return the code
     */
    public String getCode() {
        return code;
    }

    /**
     * @param code the code to set
     */
    public void setCode(String code) {
        this.code = code;
    }

    /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @param title the title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String toString() {
        return "Code: " + code + " Title: " + title; 
    }


}

如果您不想使用域类,则必须从selectedItem拆分文本,并仅打印代码部分。

帕特里克