0
不知何故,我設法使該ID不顯示在組合框中,但那麼當我按下按鈕時如何做到這一點,我可以得到ID值?按下按鈕時使用String from = (String) jComboBox1.getSelectedItem();
不起作用。我得到String code = (String) item.getValue();
我需要的id,但是如何將它傳遞給下一個查詢?如何從combobox值傳遞到
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;
}
即時通訊使用jFrame所以我添加了按鈕,然後我添加私人無效jButton1ActionPerformed(java.awt.event.ActionEvent evt),然後我應該把這個代碼? – Triax 2014-11-21 18:40:24
我不明白把代碼放在哪裏...... :( – Triax 2014-11-21 18:55:17
是的,你可以添加代碼來在你的ActionPerformed()方法中得到你的組合框的值,即在答案中代碼的最後三行: ) – vembutech 2014-11-21 19:25:11