ObservableList不会更新ArrayList [英] ObservableList doesn't update ArrayList

查看:598
本文介绍了ObservableList不会更新ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于学校作业,我们正在使用JavaFX中的ObservableList对象(对吗?).我已经为此工作了一天以上,无法解决.老师只告诉我们要使用"Google",所以这也没有帮助.

For a school assignment, we're working with ObservableList objects from JavaFX (Right?). I've been working on this for over a day now and can't figure it out. The teacher only tells us to 'Google it' so that's no help either..

基本上,我们正在开发一个基本的管理应用程序,以跟踪人们及其家人.人们是一个家庭的成员,一个家庭可以有多个成员.

Basically, we're working on a basic administration application to keep track of people and their families. Where people are member of a family, and a family can have multiple members.

添加一个人或一个家庭时,会将它们添加到一个ObservableList中,然后应该对其进行更新,以更新ArrayList(以便可以序列化数据)和GUI元素.这就是问题所在.

When a person or family is added, they are added to an observableList which should then update an ArrayList (So the data can be serialized) and a GUI element. This is where the problem is.

我们目前具有以下实现方式:

We currently have the following implementation:

private List<Persoon> personen;
private List<Gezin> gezinnen;
this.personen = new ArrayList<Persoon>();
this.gezinnen = new ArrayList<Gezin>();

private transient ObservableList<Persoon> observablePersonen;
private transient ObservableList<Gezin> observableGezinnen;
observablePersonen = FXCollections.observableArrayList(personen);
observableGezinnen = FXCollections.observableArrayList(gezinnen);

然后,在添加项目时,我们将执行以下操作:

Then when an item is added, we do the following:

Persoon p = new Persoon();
observablePersonen.add(p);
observablePersonen.notifyAll();

此后,当我们检查人员"列表时,添加的对象不存在:(

After this when we inspect the 'personen' list, the added object isn't there :(

我们缺少明显的东西吗?

Are we missing something obvious?

推荐答案

您需要使用

You need to use FXCollections.observableList instead of FXCollections.observableArrayList.

根据observableList的文档:

构造一个由指定列表支持的ObservableList.

Constructs an ObservableList that is backed by the specified list.

因此,可观察列表的任何修改都将报告给后备列表.但是,对于observableArrayList:

So any modification of the observable list will be reported to the backing list. However, in the case of observableArrayList:

创建一个新的可观察数组列表,并向其中添加集合col的内容.

Creates a new observable array list and adds a content of collection col to it.

因此此列表不受给定列表的支持,它仅用作初始集合.

so this list is not backed by the given list, it just serves as an initial collection.

请注意,您不应调用notifyAll():此方法与JavaFX无关,它与唤醒等待该对象的线程有关.

As a side note, you should not call notifyAll(): this method has nothing to do with JavaFX and it relates to waking threads waiting on this object.

这篇关于ObservableList不会更新ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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