Rom*_*Rom 2 scala javafx scalafx
我有一个表格视图。当我更新一行的属性时,我看不到修改?例如:
implicit class PersonView(p:Person) {
val fname = new ObjectProperty(this, "fname",p.name)
}
Run Code Online (Sandbox Code Playgroud)
在我的表格视图中
lazy val tableLines = ObservableBuffer(persView)
val personTable = new TableView[PersonView](tableLines) {
columns ++= List(
new TableColumn[PersonView, String] {
text = "Name"
cellValueFactory = _.value.fname
cellFactory = { _ =>
new TableCell[PersonView, String] {
item.onChange { (_, _, newValue) => text = newValue }
}
}
}
)
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但是当我更新名称时,我在 GUI 中看不到它。
首先,我将尝试总结我所看到的,以及我认为您可以如何使其发挥作用:
所述PersonView类装饰一个Person通过提供实例fname属性,则该被初始化为name相关联的字段Person。在“名称”列中创建每个单元格时,您将创建这样一个属性并将其与单元格的值相关联。此后,每当该属性的值发生变化时,单元格将自动更改其item字段以显示该属性的新值。(顺便说一句,该onChange属性是多余的和不必要的——它提供了一个机会,当item属性——即绑定的fname属性——发生变化时执行一些其他操作,因此单元格在执行时已经更新。)
因此,如果您现在更改Person实例的名称,Person“名称”列中的单元格会发生什么情况?没有。
为什么?
首先,@James_D指出,你有没有建立之间的关系name一的Person情况下,和值ObjectProperty实例原本与它相关联。也就是说,您所做的只是更改一个String值。要更新 GUI,其值也ObjectProperty需要更改。
增加您的问题的事实是,从Person到它的关联没有关系PersonView。因此,当Person name字段更改时,Personto 人员无法通知其PersonView. 更糟糕的是,通过创建PersonView一个implicit类,您暗示PersonView实例本身是不重要和短暂的,暂时存在只是为了Person用一组额外的方法和/或属性来装饰某个实例。
那么,我们怎样才能改变事情,使它们按您的预期工作呢?有两种基本方法,您的选择将取决于您可以对Person课程施加多少控制。在这两种情况下,关键是要确保StringProperty(一个更好的选择比ObjectProperty,顺便说一句)包含的名字Person每当改变name的Person改变...
首先,最简单的方法是完全取消PersonView类。显然,您需要能够进行编辑Person才能执行此操作;如果不能,则必须尝试第二种方法。Person应修改以添加fname属性字段,并name转换为报告 的当前值的函数fname:
// initName is the initial name of the Person, and may be changed later...
class Person(initName: String, /*Whatever other arguments you require*/) {
// String property storing this Person's name. Name is initialized to initName.
val fname = new StringProperty(this, "fname", initName)
// Report the current name of this Person.
def name = fname.value
// This function is not necessary, since we could change the value through fname directly
// but it does look better...
def name_=(newName: String): Unit = fname.value = newName
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您的表初始化现在如下所示:
val tableLines = ObservableBuffer(persView) // Of Person, not PersonView!
val personTable = new TableView[Person](tableLines) {
columns ++= List(
new TableColumn[Person, String] {
text = "Name"
cellValueFactory = _.value.fname
// No need for a cellFactory - default works fine.
}
)
}
Run Code Online (Sandbox Code Playgroud)
现在,您可以Person像这样更改 a 的名称:
val someone = new Person("Bob"/*, etc...*/)
someone.name = "Fred"
Run Code Online (Sandbox Code Playgroud)
一切都很好。GUI 表中相应单元格的fname属性、name字段和值现在都将具有相同的值。
如果您不能修改Person类型的定义,则需要第二种方法。在这里,我们使用PersonView来更改Person实例的名称,并希望没有人更改Person我们无法控制的名称。(也就是说,如果其他一些代码在Person没有通过的情况下修改了一个实例的名称PersonView,那么我们将一无所知,GUI 也不会相应地更新。)
PersonView,在这种情况下,不能是一个implicit类。我们希望保留一个PersonView实例并使用它与关联的Person实例进行交互。PersonView现在看起来像这样:
class PersonView(p: Person) {
// String property initialized to the name of the associated person.
val fname = new StringProperty(this, "fname", p.name)
// Change the name of the person. Note that we MUST also change the name of the
// associated person instance.
def name_=(newName: String): Unit = {
// Change the name of the Person instance. Verify it has the value we think it has.
assert(p.name == fname.value)
p.name = newName // Might be p.setName(newName), etc. in your case
// Change the name of our property.
fname.value = newName
}
}
Run Code Online (Sandbox Code Playgroud)
现在,假设您有一个Person实例列表,您需要将它们映射到PersonView实例,并随后使用这些实例。
您的 GUI 代码现在如下所示:
val tableLines = ObservableBuffer(persView)
val personTable = new TableView[PersonView](tableLines) {
columns ++= List(
new TableColumn[PersonView, String] {
text = "Name"
cellValueFactory = _.value.fname
// No need for a cellFactory - default works fine.
}
)
}
Run Code Online (Sandbox Code Playgroud)
更改人名现在有点复杂,因为我们需要能够找到正确的PersonView实例,但它看起来像这样:
val someone = new Person("Bob"/*, etc...*/)
val someoneView = new PersonView(someone)
someoneView.name = "Fred"
Run Code Online (Sandbox Code Playgroud)
一切都很好。GUI 表中相应单元格的PersonView.fname属性、Person.name字段和值(一旦someoneView添加到tableLines可观察对象)现在都将具有相同的值。
但是,以下行只是更改了Person实例的名称。在PersonView和GUI都没有得到更新:
someone.name = "Eric"
Run Code Online (Sandbox Code Playgroud)