JavaFX 2.0选择框问题.更新对象时,如何更新表示对象列表的choiceBox? [英] JavaFX 2.0 Choice Box Issue. How to update a choiceBox, which represents a list of objects, when an object is updated?

查看:120
本文介绍了JavaFX 2.0选择框问题.更新对象时,如何更新表示对象列表的choiceBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个choiceBox,它代表一个列表对象.当代表这些对象之一的名称被另一段代码更改时,选择框的下拉列表中的名称不会更改.例如,如果我有一个由列表Test对象组成的选择框.测试代码如下所示:

I have a choiceBox which represents a list objects. When the name representing one of those objects is changed by another bit of code the name in the drop down list for the choice box does not change. For example if I have a choice box which is made up of list Test objects. The Code for Test is shown below:

class Test {
    String name;

    public Test(String name) {
        this.name = name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return name; 
    }
} 

然后有一个选择框,如下所示:

Then have a choice Box as follows:

ChoiceBox<Test> chi = new ChoiceBox<>();
ObservableList<Test> items = FXCollections.observableArrayList();
chi.setItems(items);
items.addAll(new Test("ITEM1"),new Test("ITEM2"),new Test("ITEM3"));

ChoiceBox将显示ITEM1,ITEM2和ITEM3列表

The ChoiceBox will show the list ITEM1, ITEM2 and ITEM3

如果我随后通过以下代码更改其中一项的名称,则:

If I then change the name of one of the items via the following code:

items.get(1).setName("CHANGED");

ChoiceBox仍将显示ITEM1,ITEM2和ITEM3列表.我该如何做才能使choiceBox更新并显示ITEM1,CHANGED和ITEM3列表?

The ChoiceBox will still show the list ITEM1, ITEM2 and ITEM3. How can I make it so the choiceBox will update and show the list ITEM1, CHANGED and ITEM3?

推荐答案

仅出于完整性考虑-在fx2中,您可能会被替换掉其他答案中概述的方法.从fx8开始,有一种机制可以告诉列表侦听其包含项的更改(当然前提是您的项具有属性,并在更改时通知侦听器):

Just for completeness - in fx2 you are probably stuck with the replace approach as outlined in the other answer. Since fx8, there's a mechanism to tell the list to listen to changes of its contained items (precondition being, of course, that your item has properties and notifies listeners on change):

/** changed item to 
 *  - use property and notify on change
 *  - not override toString (for visuals, use converter instead)
 */ 
class Test {
    StringProperty name;

    public Test(String name) {
        setName(name);
    }

    public StringProperty nameProperty() {
         if (name == null) name = new SimpleStringProperty(this, "name");
         return name;
    }
    public void setName(String name) {
        nameProperty().set(name);
    }

    public String getName() {
        return nameProperty().get();
    }

} 

// use in collection with extractor
ObservableList<Test> items = FXCollections.observableList(
    e -> new Observable[] {e.nameProperty()} );
items.addAll(...);
choiceBox = new ChoiceBox<>(items);
// tell the choice how to represent the item
StringConverter<Test> converter = new StringConverter<Test>() {

    @Override
    public String toString(Test album) {
        return album != null ? album.getName() : null;
    }

    @Override
    public Test fromString(String string) {
        return null;
    }

};
choiceBox.setConverter(converter);

这篇关于JavaFX 2.0选择框问题.更新对象时,如何更新表示对象列表的choiceBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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