JavaFX ReadOnlyListProperty不是只读的吗? [英] JavaFX ReadOnlyListProperty not read only?

查看:147
本文介绍了JavaFX ReadOnlyListProperty不是只读的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码抛出UnsupportedOperationException,正如我所料,因为它是只读的。

This code throws an UnsupportedOperationException, as I would expect it to since it's read only.

ListProperty<String> lp = new SimpleListProperty<String>();
ReadOnlyListWrapper<String> rolw = new ReadOnlyListWrapper<String>(lp);
ReadOnlyListProperty<String> rolp = rolw.getReadOnlyProperty();
rolp.add("element");

但是,此代码没有。

ObservableList<String> ol = FXCollections.observableArrayList();
ReadOnlyListWrapper<String> rolw = new ReadOnlyListWrapper<String>(ol);
ReadOnlyListProperty<String> rolp = rolw.getReadOnlyProperty();
rolp.add("element");

这是一个错误,还是我只是不明白?

Is this a bug, or am I just not understanding something?

推荐答案

对于提供的示例,原始期望是错误的。 UnsupportedOperationException由于其他原因而发生,而不是因为只读列表被写入。仍然可以使用只读列表。我希望下面的答案有助于澄清。

The original expectation is wrong - for the examples provided. The UnsupportedOperationException occurs for a different reason and not because a "read-only" list is being "written" to. It is still possible to have "read-only" lists. I hope the answer below helps clarify.

答案需要分为两部分。一个:ListProperty异常和两个:只读列表。

The answer needs to be considered in two parts. One: The ListProperty exception and Two: Read-Only lists.

1)ListProperty示例失败,因为没有为该属性分配列表。

1) The ListProperty example fails because no list has been assigned to the property.

这个简化的示例也会引发异常。请注意,删除了任何只读方面:

This, simplified, example also throws the exception. Note that any "read-only" aspects are removed:

ListProperty<String> lp = new SimpleListProperty<>();
lp.add("element");

可以通过以下方式更正:

This can be corrected with:

ObservableList ol = FXCollections.observableArrayList();
ListProperty<String> lp = new SimpleListProperty<>();
lp.setValue(ol);
lp.add("element");

如果我们以类似的方式更改原始示例,那么ListProperty和ObservableList示例都不会抛出异常,这不是OP想要或期望的。

If we change the original example in a similar manner then neither the ListProperty nor the ObservableList examples will throw an exception, which wasn't what the OP wanted or expected.

2)第二部分询问为什么可以将元素添加到只读列表中。使用FXCollections.unmodifiableObservableList创建只读列表将按预期抛出UnsupportedOperationException:

2) The second part asks why can an element be added to a read-only list. Using FXCollections.unmodifiableObservableList to create the read-only list will throw UnsupportedOperationException as expected:

ObservableList<String> ol = FXCollections.observableArrayList();
ObservableList<String> uol = FXCollections.unmodifiableObservableList(ol);
uol.add("element");

但这不能回答为什么ReadOnlyListWrapper / Property没有这样做的问题?

But this doesn't answer the question why doesn't the ReadOnlyListWrapper/Property do this?

让我们先处理该物业。 ListProperty允许更改,即它允许您为属性分配不同的列表。 ReadOnlyListProperty不允许这样做,即一旦分配了列表,它就是该列表对象。列表的内容仍然可以更改。 ReadOnlyListProperty对下面的例子毫无意义:

Let's deal with the Property first. ListProperty enable the value to be changed, i.e. it allows you to assign a different list to the property. ReadOnlyListProperty does not allow this, i.e. once a list is assigned it remains that list object. The content of the list can still change. The example below makes no sense with ReadOnlyListProperty:

ObservableList<String> ol1 = FXCollections.observableArrayList();
ObservableList<String> ol2 = FXCollections.observableArrayList();
ListProperty<String> lp = new SimpleListProperty<>(ol1);
lp.setValue(ol2);

所以只读是指属性,而不是列表。

So read-only refers to the Property, and not the List.

最后 - ReadOnlyListWrapper - 正如API文档所述这个类提供了一个方便的类来定义只读属性。它创建了两个同步的属性。一个属性是只读的,可以传递给外部用户。另一个属性是可读写的,只能在内部使用。

Finally - ReadOnlyListWrapper - as the API documentation states "This class provides a convenient class to define read-only properties. It creates two properties that are synchronized. One property is read-only and can be passed to external users. The other property is read- and writable and should be used internally only."

这篇关于JavaFX ReadOnlyListProperty不是只读的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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