【问题标题】:How to hide jcombobox item from list but still use it to get data from databse tables如何从列表中隐藏 jcombobox 项目但仍使用它从数据库表中获取数据
【发布时间】:2014-11-20 15:29:31
【问题描述】:

你好,所以不知何故设法使该 id 没有显示在组合框中,但是当我按下按钮时如何使我可以得到 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 jquery database swing jcombobox


【解决方案1】:

您需要创建一个自定义对象来包含您的数据,然后将自定义对象添加到模型中。然后您需要使用自定义渲染器来显示此对象。

查看Combo Box With Custom Renderer 以获取有关如何执行此操作的示例。

【讨论】:

  • 对不起,我是新手,我读了 Combo Box With Custom Renderer 几次,但仍然不明白该怎么做。而且我确实阅读了有关如何使用 JCombobox 的信息,但我仍然缺少一些东西:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-04
  • 1970-01-01
  • 2020-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-17
相关资源
最近更新 更多