Combobox JavaFX8:如果组合框的当前选定项被修改,则更新该项

Combobox JavaFX8:如果组合框的当前选定项被修改,则更新该项,combobox,javafx-8,changelistener,observablelist,Combobox,Javafx 8,Changelistener,Observablelist,我有一个组合框,其中有一个可观察列表作为模型。我可以从外部更新列表的元素,如果修改了,ComboBox的当前选定项,我想更新它。 我试着做: private final ObservableList<Item> list; private ComboBox<Item> comboBox; .... comboBox.setItems(list); comboBox.getSelectionModel().selectFirst(); System.out.prin

我有一个
组合框
,其中有一个
可观察列表
作为模型。我可以从外部更新列表的元素,如果修改了,
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);
私有最终可观察列表;
专用组合框组合框;
....
comboBox.setItems(列表);
comboBox.getSelectionModel().selectFirst();
System.out.println(“选定索引:+comboBox.getSelectionModel().getSelectedIndex(););
ListChangeListener=新建ListChangeListener(){
@凌驾

public void onChanged(ListChangeListener.Change不明白您的目的是什么-在代码段中,您似乎重新选择了已选择的项…为什么?无论如何,过程与往常一样:请显示SSCCE以演示您的问题:-)当外部更改时,我会重新选择所选项目。例如,如果我的组合框包含字符串a-B-C-D和所选项目a的列表。在我的应用程序中,我有一个可以添加、编辑和删除列表的部分。如果我在A1中编辑a,我希望组合框的所选项目反映更改。无论如何,我将尝试创建一个简单的示例。谢谢@kleopatra:-)