ComboBox奇怪的行为(JavaFX 8) [英] ComboBox strange behaviour (JavaFX 8)

查看:728
本文介绍了ComboBox奇怪的行为(JavaFX 8)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在start方法中有这个代码:

I have this code in start method:

ObservableList<StringBuilder> list = FXCollections.observableArrayList();
list.add(new StringBuilder("0"));
list.add(new StringBuilder("1"));
list.add(new StringBuilder("2"));
list.add(new StringBuilder("3"));
list.add(new StringBuilder("4"));
list.add(new StringBuilder("5"));
list.add(new StringBuilder("6"));
list.add(new StringBuilder("7"));
list.add(new StringBuilder("8"));
list.add(new StringBuilder("9"));

ComboBox<StringBuilder> combo = new ComboBox<>(list);
Button change = new Button("change");
change.setOnAction((event) -> {
    list.set(5, new StringBuilder("-"));
});

BorderPane borderPane = new BorderPane(combo, null, null, change, null);
Scene scene = new Scene(borderPane);
primaryStage.setScene(scene);
primaryStage.show();

我启动程序并选择第二个项目,然后点击更改按钮几次。没有什么发生,这是好的。
但是如果我选择项目6并点击更改按钮,然后组合框自动选择下一个项目(7)。
为什么会发生?

I start my program and select second item and then I click on "change" button several times. Nothing happens and it's good. But If I select item "6" and click on "change" button then comboBox autoselects next item ("7"). Why does it happen?

然后我们可以更改

list.set(5,new StringBuilder ( - );

list.remove(5);

然后如果我选择项目2更改按钮,然后没有任何反应。如果我选​​择项目9并点击更改按钮,然后组合框自动选择上一个项目(8)。

Then if I select item "2" and click "change" button then nothing happens. And again if I select item "9" and click "change" button then comboBox autoselects previous item ("8").

如果我只是改变item,即使用set方法的列表,那么comboBox不改变选择。如何处理它?<​​/ p>

I expect that if I just change item, i.e. use set method of list, then comboBox doesn't change selection. How to cope with it?

推荐答案

我不知道为什么会发生这种情况,这可能与 ComboBox 处理 ObservabeList 但是这不能使用 ChoiceBox 来复制。你能请举个 JIRA 与他们的JavaFX团队,以便他们可以看看吗?

I am not sure why this is happening, this might have something to do with the way ComboBox handles ObservabeList in it. But this cannot be replicated using ChoiceBox. Can you please raise a JIRA with the JavaFX team, so that they can have a look at it?

作为周转,您可以在代码中执行以下操作以临时修复它:

As a turnaround, you may do the following in your code to fix it temporarily :

ComboBox<String> combo = new ComboBox<String>(list);
Button change = new Button("change");
change.setOnAction((event) -> {
    StringBuilder str = combo.getValue();
    list.set(5, new String("-"));
    combo.setValue(str);
});

这篇关于ComboBox奇怪的行为(JavaFX 8)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆