为什么JavaFX table.getItems()。clear()也清除ObservableList [英] Why does JavaFX table.getItems().clear() clear the ObservableList as well

查看:2139
本文介绍了为什么JavaFX table.getItems()。clear()也清除ObservableList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JavaFX表定义为:

I've a JavaFX table defined as:

TableView<Person> table = new TableView<>;
//Person Class contains firstName,lastName & email properties
//table has three columns First Name, Last Name & Email

ObservableList Person s

ObservableList<Person> persons = FXCollections.observableArrayList();

后台服务,该任务是通过添加条目动态填充表( Person objects)到 ObservableList people

A background service, which task is to populate the table dynamically by adding entries(Person objects) to the ObservableList persons.

表被绑定为: / p>

table is binded as:

table.itemsProperty().bind(service.valueProperty());

一切都正常...但我最近发现,如果我清除表项

Everything is working fine... but I've recently found that if I clear the table items by

table.getItems().clear();

它不仅清除表,还可以清除 ObservableList个人。我想知道这是否是双向绑定?
即使我在调用表$ code> getItems()。clear()
之前解除绑定,它也产生相同的结果。
有人可以解释我没有得到什么意见吗?

It does not only clears the table, but also the ObservableList persons. I'm wondering if this a bidirectional binding? Even if I unbind it before calling getItems().clear() on the table, it produces same result. Could someone explain what the point I'm not getting here?

推荐答案

当您设置 itemsProperty 的一个 TableView ,你实际在做的是设置存储在属性中的引用(指针),指向指定的 ObservableList ,而是指定列表的精确副本。

When you set the itemsProperty of a TableView, what you are actually doing is to set the reference (pointer) stored in the property, to point to the specified ObservableList, rather that making an exact copy of the specified list.

因此,当您调用 getItems ,它返回指定列表的引用。

Therefore, when you call getItems, it returns the reference to the specified list.

因此,如果调用 clear()在此列表中,您将清除原始 ObservableList

Therefore, if you call clear() on this list, you will clear the original ObservableList.

注意:

在大多数情况下绑定你所尝试的东西是没有必要的:

In most of the cases the binding what you have tried is not necessary:

table.itemsProperty().bind(service.valueProperty());

只要设置 itemsProperty 像

table.setItems(service.valueProperty().get());

只有在计划存储在 valueProperty 将在运行时指向另一个列表。在大多数情况下,表格会收到有关列表更改的通知。

The binding is only necessary if you plan that the reference stored in valueProperty will point to another list in runtime. In most of the cases it is enough that the table gets notifications on the changes of the list.

这篇关于为什么JavaFX table.getItems()。clear()也清除ObservableList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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