【问题标题】:how to pass from combobox value to如何从组合框值传递到
【发布时间】:2014-11-21 17:52:17
【问题描述】:

不知何故,我设法使该 id 没有显示在组合框中,但是当我按下按钮 ok 时如何使我可以获得 id 值?按下按钮时使用String from = (String) jComboBox1.getSelectedItem(); 不起作用。我得到了String code = (String) item.getValue(); 我需要的 id,但是如何将它传递给下一个查询?

public void select() {

         try {
            String sql = "select * from category";
            pst = con.prepareStatement(sql);
            rs = pst.executeQuery();

            while (rs.next()) {

        jComboBox1.addItem(new Item<String>(rs.getString("mkid"), rs.getString("name")));

            }

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }

        jComboBox1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JComboBox jComboBox1 = (JComboBox) e.getSource();
                Item item = (Item) jComboBox1.getSelectedItem();
                String code = (String) item.getValue();
                System.out.println(code);
            }
        });


    }


    The item

     public class Item<V> implements Comparable<Item>
     {
    private V value;
    private String description;


    public Item(V value, String description)
    {
        this.value = value;
        this.description = description;
    }


    public V getValue()
    {
        return value;
    }

    public String getDescription()
    {
        return description;
    }

    public int compareTo(Item item)
    {
        return getDescription().compareTo(item.getDescription());
    }

    @Override
    public boolean equals(Object object)
    {
        Item item = (Item)object;
        return value.equals(item.getValue());
    }

    @Override
    public int hashCode()
    {
        return value.hashCode();
    }


    @Override
    public String toString()
    {
        return description;
    }

【问题讨论】:

    标签: java swing combobox jcombobox


    【解决方案1】:

    为您的按钮添加一个侦听器,然后添加代码以将您的组合框值转换为字符串。

           JButton okButton = new JButton("OK");  
           okButton.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                JComboBox jComboBox1 = (JComboBox) e.getSource();
                Item item = (Item) jComboBox1.getSelectedItem();
                String code = (String) item.getValue();
                System.out.println(code);
             }          
          });
    

    【讨论】:

    • 我在使用 jFrame 所以我添加了按钮然后我添加了 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 然后我应该把这个代码?
    • 是的,您可以在 ActionPerformed() 方法中添加代码以单独获取组合框值。即答案中代码的最后三行:)
    • okey 所以它看起来像这样:private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { JComboBox jComboBox1 = (JComboBox) evt.getSource(); Item item = (Item) jComboBox1.getSelectedItem(); String code = (String) item.getValue(); } 现在我尝试在我的查询中使用字符串代码,但我得到了大量的错误。 Querry 看起来pst = con.prepareStatement("select * from table where mk_id = '" + code + "'");还有一些错误,比如java.lang.ClassCastException:javax.swing.JButton cannot be cast to javax.swing.JComboBox
    • JComboBox jComboBox1 = (JComboBox) evt.getSource();这使得例外。既然已经拿到了选中的item值,就可以单独使用了
    • 那么单独怎么用呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多