我在使用 JComboBox 的 setSelectedItem 方法时遇到问题。它只是不工作。这是我的代码;我的 Station 和 Division 课程还可以。所以它们不包括在内。无论我尝试什么,我都无法在 ComboBox 中将项目设置为选中;
//ListItem Class
public class ListItem {
private String id;
private String value;
public ListItem()
{
id = "";
value = "";
}
public ListItem(String id, String value)
{
this.id = id;
this.value = value;
}
public String getID(){return this.id;}
public String getValue() { return this.value;}
public void setID(String id ){this.id = id;}
public void setValue(String value) {this.value = value;}
@Override
public String toString()
{
return value;
}
//show station
private void showSelectedStation(String stationCode){
stationDA = new StationDA();
station = stationDA.getStationByCode(stationCode);
//Assign values to Division fields
jtxtStationCode.setText(Integer.toString(station.getStationCode()));
jtxtStationName.setText(station.getStationName());
divisionDA = new DivisionDA();
division = divisionDA.getDivisionByCode(station.getDivisionCode());
ListItem myItem = new ListItem(
division.getDivisionCode(), division.getDivisionName());
jcbDivisionName.setSelectedItem(myItem); //not working.Only the first Value in the list is showing
}