0
私は2つのJComboBoxes
を持っています。もう1つはユーザの選択に基づいて値を動的に変更します。私は正しくidを選択しました。しかし、私のJComboBox
の最初の要素を得ることは、私にItemListener
を発射せずにidを取得したいゼロ値を私に与えます。選択したアイテムのIDを取得する0
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);
}
}
}
ユーザーが選択した値を変更するたびに、 DefaultComboBoxModel
も対応するIDでその値が変更されました。しかし、それは私を返す0
を返します。