I have combobox which is filled with some items. Each item have display member and value member:
Vector model = new Vector();
model.addElement(new FilterValue("10000 Hz", 0));
model.addElement(new FilterValue("5000 Hz", 1));
model.addElement(new FilterValue("1000 Hz", 5));
model.addElement(new FilterValue("100 Hz", 50));
model.addElement(new FilterValue("10 Hz", 500));
model.addElement(new FilterValue("1 Hz", 5000));
public class FilterValue {
private final String label;
private final int value;
public FilterValue(String label, int value) {
this.label = label;
this.value = value;
}
public String getLabel() {
return label;
}
public int getValue() {
return value;
}
public String toString()
{
return label;
}
}
Initialization of JComboBox
cbFilter1 = new JComboBox(model);
cbFilter1.setBounds(176, 70, 90, 20);
cbFilter1.setSelectedIndex(-1);
pnlOUT1.add(cbFilter1);
cbFilter1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox comboBox = (JComboBox)e.getSource();
FilterValue item = (FilterValue)comboBox.getSelectedItem();
System.out.println( item.getValue() + " : " + item.getLabel() );
}
});
When I select for example 5000 Hz, display text is 5000 Hz, and value is 1.
The question is how to set value for example 5 and display 1000 Hz?
I have tried with
cbFilter1.setSelectedItem(5);
But there is no effect.