简体   繁体   English

绑定两个不同对象的 ObservableList。 (没有 EasyBind)

[英]Binding two ObservableLists of different Objects. (Without EasyBind)

ObservableList<String> src = FXCollections.observableArrayList();
ObservableList<Integer> other = FXCollections.observableArrayList();
//Create binding.
src.addAll("1", "2", "3");
System.out.println(other);//Should print [1, 2, 3].

Above I have an idea of what I am trying to do.以上我对我正在尝试做的事情有一个想法。

As items are added and removed to the src list, the other list is synchronized along with it.在 src 列表中添加和删除项目时,另一个列表也随之同步。 Every item in the src list has an equivalent to the items in the other list. src 列表中的每个项目都与另一个列表中的项目等效。 The other list contains "translated" values from the src list, of course.当然,另一个列表包含来自 src 列表的“翻译”值。

Many have recommended using EasyBind, but I would like to understand how to do this manually first.许多人建议使用 EasyBind,但我想先了解如何手动执行此操作。

You could use a listener for this like in this example:您可以在此示例中使用侦听器:

ObservableList<String> src = FXCollections.observableArrayList();
ObservableList<Integer> other = FXCollections.observableArrayList();
// Create binding.
src.addListener(new ListChangeListener<String>() {
    @Override
    public void onChanged(ListChangeListener.Change<? extends String> change) {
        while (change.next()) {
            for (String changedString : change.getList()) {
                other.add(Integer.valueOf(changedString));
            }
        }
    }
});
src.addAll("1", "2", "3");
System.out.println(other); // Prints out [1, 2, 3].

Or you can make a method for adding to List, like:或者您可以创建一个添加到 List 的方法,例如:

private void addToList(String s) {
    src.add(s);
    other.add(Integer.valueOf(s));
}

Happy Coding,快乐编码,
Kalasch卡拉什

Jackson (Java):反序列化相同的属性名但返回不同的对象。 其中一个返回 object 和第二个列表<object><div id="text_translate"><p>我有一个这样的 POJO:</p><pre> public class NewClass { String name; @JsonProperty("productType") ProductType productType2005; List&lt;ProductType&gt; productType; }</pre><p> 我想将 json 反序列化为 Pojo。 问题是我的属性名称相同 productType 但我可以期待两种不同的返回类型或数据结构。</p><ol><li> 返回ProductType</li><li> return List&lt;ProductType&gt;因为属性名称相同我如何有效地使用 Jackson 注释来解决它?</li></ol><p> 我使用 rest-assured 进行反序列化,使用 Lombok 进行典型的 getter 和 setter。</p></div></object> - Jackson (Java) : deserialization for the same property name but return different objects. for one it return object and second List<Object>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较Junit中的两个observable列表 - Comparing two observableLists in Junit 绑定到 ObservableValue<ObservableList> 用 EasyBind 代替 ObservableList - Binding to a ObservableValue<ObservableList> instead of an ObservableList with EasyBind 如何为两个对象使用Collections方法(removeAll()和retainAll())。 (对象是父子关系) - How to use Collections methods(removeAll() and retainAll()) for two objects. (objects are parent-child relation) Jackson (Java):反序列化相同的属性名但返回不同的对象。 其中一个返回 object 和第二个列表<object><div id="text_translate"><p>我有一个这样的 POJO:</p><pre> public class NewClass { String name; @JsonProperty("productType") ProductType productType2005; List&lt;ProductType&gt; productType; }</pre><p> 我想将 json 反序列化为 Pojo。 问题是我的属性名称相同 productType 但我可以期待两种不同的返回类型或数据结构。</p><ol><li> 返回ProductType</li><li> return List&lt;ProductType&gt;因为属性名称相同我如何有效地使用 Jackson 注释来解决它?</li></ol><p> 我使用 rest-assured 进行反序列化,使用 Lombok 进行典型的 getter 和 setter。</p></div></object> - Jackson (Java) : deserialization for the same property name but return different objects. for one it return object and second List<Object> 在不知道格式的情况下解析JSON(可以是两个不同的对象之一) - Parse JSON without knowing format (can be one of two different objects) 假设我有 5 个对象。 我希望这个特定对象的数据可以被 2 个对象访问,而不是其他两个。 我怎样才能做到这一点? - Suppose I have 5 objects. I want the data of this particular object be accessible by 2 objects but not the other two. How can I do that? CDI @New on @Singleton对象。 它能做什么? - CDI @New on @Singleton objects. What it does? HashSet类对象。 什么是哈希? - HashSet of class objects. What is hashed? 对象列表。 使用整数条目的操作 - List of objects. Operations with Integer entries Hibernate不会删除我的对象。 为什么? - Hibernate is not deleting my objects. Why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM