私はas モデルをComboBox
持っています。ObservableList<Item>
リストの要素を外部から更新できます。現在選択されているアイテムがComboBox
変更されている場合は、それを更新したいと考えています。私はやろうとしました:
private final ObservableList<Item> list;
private ComboBox<Item> comboBox;
....
comboBox.setItems(list);
comboBox.getSelectionModel().selectFirst();
System.out.println("selected index: " + comboBox.getSelectionModel().getSelectedIndex(); );
ListChangeListener<Item> listener = new ListChangeListener<Item>() {
@Override
public void onChanged(ListChangeListener.Change<? extends Item> c) {
while (c.next()) {
for (int i = c.getFrom(); i < c.getTo(); ++i) {
int selectedIndex = comboBox.getSelectionModel().getSelectedIndex();
System.out.println("selected index: " + selectedIndex );
if (i == selectedIndex) {
comboBox.getSelectionModel().select(selectedIndex);
}
}
}
}
};
list.addListener(listener);
ChangeListener
リストにを追加したので、 の要素list
が更新されたときに、 で選択したアイテムを更新できますcomboBox
。しかし、これは機能しません。comboBox.getSelectionModel().getSelectedIndex()
内部onChange
が返さ-1
れ、その理由がわかりません (最初の System.out など、onChange から値を出力すると、値は正しいです)。
この振る舞いを説明してもらえますか? また、私が望むものを達成するためのより良い方法があれば教えてください。ありがとう。