我是ScalaFx的新手,想要创建一个包含带CheckBox
s的可编辑布尔列的TableView(以及带有TextFields的可编辑String和Int列)。
我假设我需要使用CheckBoxTableCell
。
我正在使用JDK 7u25,ScalaFX 1.0.0-M4 / M5和Scala 2.10.2-final。 (我不完全确定ScalaFX版本,但它肯定至少是1.0.0-M5。无论如何,它是8月1日上传到J https://oss.sonatype.org/index.html#nexus-search;quick~scalafx的快照.Jarek只编译Scala 2.9.x,但是我已经下载了他的源代码并重新编译了它。)
我已经设法在Integer Columns in ScalaFX TableView,http://foofighter2146.blogspot.com/2013/06/tutorial-scalafx-slick.html和scalafx-demo:SimpleTableView基础上完成了一半的工作。但是,我不能在TableView中使用CheckBox并使用它们的值。相反,我只能以这样的方式让它工作,我需要输入“true”或“false”来编辑表中的值。
到目前为止,我已经成功实现了目标:
class Person(firstName_ : String, age_ : Int, cool_ : Boolean) {
val name = new StringProperty(this, "Name", firstName_)
val age = new IntegerProperty(this, "Age", age_)
val cool = new ObjectProperty(this, "Cool", cool)
}
object SimpleEditableTableView extends JFXApp {
val characters = ObservableBuffer[Person](
new Person("Peggy", 45, false),
new Person("Rocky", 43, true)
)
stage = new PrimaryStage {
title = "Simple Ediatble Table View"
scene = new Scene {
content = new TableView[Person](characters) {
editable = true
columns ++= List(
new TableColumn[Person, String] {
text = "Name"
cellValueFactory = {_.value.name}
cellFactory = _ => new TextFieldTableCell[Person, String] (new DefaultStringConverter())
onEditCommit = (evt: CellEditEvent[Person, String]) => {
val person = evt.rowValue
val newName = evt.newValue
println("Here we'll typically save the data. New name = "+newName)
}
editable = true
prefWidth = 180
},
new TableColumn[Person, Int] {
text = "Name"
cellValueFactory = {_.value.age}
cellFactory = _ => new TextFieldTableCell[Person, Int] (new IntStringConverter())
onEditCommit = (evt: CellEditEvent[Person, Int]) => {
val person = evt.rowValue
val newAge = evt.newAge
println("Here we'll typically save the data. New age = "+newAge)
}
editable = true
prefWidth = 180
},
new TableColumn[Person, Boolean] {
text = "Cool"
cellValueFactory = {_.value.cool}
cellFactory = _ => new TextFieldTableCell[Person, Boolean] (new BooleanStringConverter())
onEditCommit = (evt: CellEditEvent[Person, Boolean]) => {
val person = evt.rowValue
val newCool = evt.newCool
println("Here we'll typically save the data. New cool = "+newCool)
}
editable = true
prefWidth = 180
}
)
}
}
}
}
对于“酷”TableColumn,我想替换
cellFactory = _ => new TextFieldTableCell[Person, Boolean] (new BooleanStringConverter())
与
val selectedProperty: Int => ObservableValue[Boolean, java.lang.Boolean] = {rowNum: Int => model.characters(rowNum).cool}
cellFactory = column => CheckBoxTableCell.forTableColumn[Person, Boolean](selectedProperty)
为了在TableView中获得漂亮的CheckBoxes(目前,我得到的TextFields必须输入“true”或“false”)。但是,这给了我(明显的)错误:
type mismatch; found : scalafx.beans.property.ObjectProperty[scala.Boolean] required: scalafx.beans.value.ObservableValue[scala.Boolean,java.lang.Boolean]
如果我改变
val selectedProperty: Int => ObservableValue[Boolean, java.lang.Boolean] = {rowNum: Int => model.characters(rowNum).cool}
to val selectedProperty = {rowNum:Int => model.characters(的rowNum).cool}
我收到一条错误消息,这基本上归结为CheckBoxTableCell.forTableColumn[Person, Boolean](selectedProperty)
要求selectedProperty
为Int => ObservableValue[Boolean, java.lang.Boolean]
类型,而不是Int => ObjectProperty[Boolean]
答案 0 :(得分:1)
基本上你应该使用BooleanProperty,你可以在scalafx-users讨论组发布的同一个问题的答案中找到详细信息,包括一个完整的例子 https://groups.google.com/d/msg/scalafx-users/oedbP9TY5YE/csNi1qhsNuQJ