何时在String上使用StringProperty? [英] When to use StringProperty over String?
问题描述
我正在JavaFX中开发一个应用程序,我希望在单个 Person
类中表示Person的信息。我遇到了一个教程,其中一个人的名字被表示为 StringProperty
而不是 String
。我已经搜索了这些差异,并找到了这个和这个但是我的解释还不足以让我抓住这个概念。网上的一些帖子表示,使用 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
而不是字符串
这样做有什么好处?
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 over String firstName?
When to use StringProperty firstName over String firstName?
当其他人观察到 firstName
变量时使用它。你也可以通过附加一个监听器来观察它。您可以在与JavaFX的其他可观察对象的绑定中使用此变量。在某些情况下,必须使用JavaFX属性,例如使用可编辑的tableView呈现的Person列表。要在编辑的单元格中立即反映更改,基础边界字段应该是属性。
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.
这篇关于何时在String上使用StringProperty?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!