Here is the scenario: I have a table in database with 3 columns (id, name, age). I've created 3 swing comboboxes and a button that sends a "select statement" to the database and fills the comboboxes out with addItem(...)
.
My ActionEv开发者_运维知识库ent for the button:
jComboBox1.addItem(search.getInt("ID"));
jComboBox2.addItem(search.getString("NAME"));
jComboBox3.addItem(search.getString("AGE"));
** search
is the ResultSet I acquire!
Thanks in advance.
You should implement a custom ComboBoxModel for such operations.
You can put the logic of your choices inside setSelectedItem method:
public class YourComboBoxModel implements ComboBoxModel{
public void setSelectedItem(Object anItem){
}
public Object getSelectedItem() {...}
public Object getElementAt(int index){...}
public int getSize() {...}
}
and add the desired ComboBoxModel to the relative JComboBox:
YourComboBoxModel model = new YourComboBoxModel();
JComboBox box = new JComboBox();
box.setModel(model);
精彩评论