scala - ScalaFX TableView 中的可编辑 bool 列

标签 scala javafx-2 scalafx

我是 ScalaFx 的新手,想创建一个包含可编辑 bool 列的 TableView,其中包含 CheckBox s(以及带有 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 编译,但我已经下载了他的源代码并重新编译了它。)

我已经设法根据 Integer Columns in ScalaFX TableView 让它工作到一半, http://foofighter2146.blogspot.com/2013/06/tutorial-scalafx-slick.html和 scalafx 演示: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}

到 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-users讨论组发布的同一问题的答案中找到详细信息,包括一个完整的例子
https://groups.google.com/d/msg/scalafx-users/oedbP9TY5YE/csNi1qhsNuQJ

关于scala - ScalaFX TableView 中的可编辑 bool 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18207446/

相关文章:

scala 为抽象数据类型正确定义了一个空值

javafx-2 - 在JavaFX2.2中,如何设置输入字段和表格标题中输入的文本的字体大小?

java - 设置拍摄快照的透明背景

scalafx + intellij : NoClassDefFoundError: javafx/scene/shape/CullFace

java - 调用一个从scala代码接受 `<? super T>`类型参数的java方法?

JavaMail API 将附件作为转发邮件发送给发件人

scala - 当实现只是抛出新错误时,Scala Array apply 方法如何返回索引处的值

image - JavaFX 2.2 图像支持 .ico?

java - 无法在新场景中移动 TextFlow

scala - Scala 中的方法与函数和隐式