何时在字符串上使用 StringProperty? [英] When to use StringProperty over String?
问题描述
我正在 JavaFX 中开发一个应用程序,我想在一个 Person
类中表示一个人的信息.我遇到了一个教程,其中一个人的名字被表示为 StringProperty
而不是 String
.我已经搜索了这些差异并发现了 this 和 this 但那里的解释不足以让我抓住这个概念.网上的一些帖子说使用 StringProperty
比使用 String
有优势,但没有提到它们.
I am developing an application in JavaFX where I want to represent information of a Person in a single Person
class. I came across a tutorial where a Person's name was being represented as a StringProperty
instead of String
. I have searched for the differences of these and found out this and this but the explanation there is not sufficient for me to grab the concept. Some post on the net said there are advantages of using StringProperty
over String
but could not mention them.
现在我的问题是:什么条件要求一个人使用 StringProperty
而不是 String
,这样做有什么好处?
Now my question is: What conditions require one to use StringProperty
over String
and what are the advantages of doing that?
为什么会这样:
StringProperty firstName;
在此之上:
String firstName;
推荐答案
何时使用 StringProperty firstName 而不是 String firstName?
When to use StringProperty firstName over String firstName?
当这个 firstName
变量将被其他人观察时使用它.您也可以通过附加侦听器来观察它.您可以在与 JavaFX 的其他可观察对象的绑定中使用此变量.在某些情况下,必须使用 JavaFX 属性,例如使用可编辑的 tableView 呈现的人员列表.要在编辑的单元格中立即反映更改,底层绑定字段应该是一个属性.
Use it when this firstName
variable is going to be observed by others. You also can observe it by attaching a listener. You can use this variable in bindings with other observable objects of JavaFX. In some circumstances it is mandatory to use JavaFX Property, like Person list rendered with tableView which is editable. To reflect the changes immediately in edited cell, the underlying bound field should be a property.
这篇关于何时在字符串上使用 StringProperty?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!