[英]Editable Boolean Columns in ScalaFX TableView
我是ScalaFx的新手,想創建一個TableView,其中包含一個帶有CheckBox
的可編輯布爾列(以及帶有TextFields的可編輯String和Int列)。 我假設我需要使用CheckBoxTableCell
。 我正在使用JDK 7u25,ScalaFX 1.0.0-M4 / M5和Scala 2.10.2-final。 (我不確定ScalaFX的版本,但肯定至少是1.0.0-M5。無論如何,這是Jarek於8月1日上傳到https://oss.sonatype.org/index.html#nexus的快照-search; quick〜scalafx。Jarek只為Scala 2.9.x編譯,但是我已經下載了他的源代碼並重新編譯了它。)
我已經設法根據ScalaFX TableView中的Integer Columns , 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
}
)
}
}
}
}
對於“ Cool” 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(當前,我得到了TextField,必須在其中鍵入“ 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}
到val selectedProperty = {rowNum:Int => model.characters(rowNum).cool}
我收到一條錯誤消息,基本上可以歸結為以下事實: CheckBoxTableCell.forTableColumn[Person, Boolean](selectedProperty)
要求selectedProperty
的類型為Int => ObservableValue[Boolean, java.lang.Boolean]
,而不是Int => ObjectProperty[Boolean]
基本上你應該使用BooleanProperty,你可以找到的細節,包括一個完整的例子,在回答同樣的問題張貼scalafx用戶討論組https://groups.google.com/d/msg/scalafx-users/oedbP9TY5YE/ csNi1qhsNuQJ
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.