I have two JComboBoxes
where the other one dynamically changing the value based on user choices. I properly get the id of those selected item. But getting the first element of my JComboBox
gives me a zero value where I wanted to get the id without firing the ItemListener
.
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("Add".equals(e.getActionCommand())) {
System.out.println("Position id is " + getPositionId());
}
}
}
ItemListener itemListener = new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == department) {
Department item = (Department) department.getSelectedItem();
int departmentId = item.getId();
model.setDeptId(departmentId);
List<Position> list = model.getAllPositionId();
DefaultComboBoxModel positionModel = new DefaultComboBoxModel(list.toArray());
position.setModel(positionModel);
}
else if (e.getSource() == position) {
Position item = (Position) cbPosition.getSelectedItem();
int positionId = item.getId();
model.setPositionId(positionId);
}
}
}
Every time the user changed the selected value. DefaultComboBoxModel
changed also its value with corresponding id. But getting it returns me 0
.
I already found a solution. I just created a void method outside the ItemListener
where it gets the first element.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.