简体   繁体   English

ScalaFX TableView中的可编辑布尔列

[英]Editable Boolean Columns in ScalaFX TableView

I'm new to ScalaFx and want to create a TableView containing an editable Boolean column with CheckBox s (as well as editable String and Int columns with TextFields). 我是ScalaFx的新手,想创建一个TableView,其中包含一个带有CheckBox的可编辑布尔列(以及带有TextFields的可编辑String和Int列)。 I assume I'll need to use CheckBoxTableCell . 我假设我需要使用CheckBoxTableCell I'm using JDK 7u25, ScalaFX 1.0.0-M4/M5 and Scala 2.10.2-final. 我正在使用JDK 7u25,ScalaFX 1.0.0-M4 / M5和Scala 2.10.2-final。 (I'm not completely certain about the ScalaFX version, but it's definately at least 1.0.0-M5. In any case it's the snapshot Jarek uploaded on August 1 to https://oss.sonatype.org/index.html#nexus-search;quick~scalafx . Jarek only compiles for Scala 2.9.x, but I've downloaded his source code and recompiled it.) (我不确定ScalaFX的版本,但肯定至少是1.0.0-M5。无论如何,这是Jarek于8月1日上传到https://oss.sonatype.org/index.html#nexus的快照-search; quick〜scalafx。Jarek只为Scala 2.9.x编译,但是我已经下载了他的源代码并重新编译了它。)

I've already managed to get it halfway working based on Integer Columns in ScalaFX TableView , http://foofighter2146.blogspot.com/2013/06/tutorial-scalafx-slick.html and the scalafx-demo: SimpleTableView. 我已经设法根据ScalaFX TableView中的Integer Columnshttp ://foofighter2146.blogspot.com/2013/06/tutorial-scalafx-slick.html和scalafx-demo:SimpleTableView使其工作一半。 However, I can't use CheckBox's in the TableView and use their values. 但是,我不能在TableView中使用CheckBox并使用它们的值。 In stead, I can only get it to work in such a way that I need to type in "true" or "false" to edit the value in the table. 相反,我只能使其以某种方式工作,即需要输入“ true”或“ false”来编辑表中的值。

Here's what I have managed to get working thus far: 到目前为止,这是我设法完成的工作:

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
          }
        )
      }
    }
  }
}

For the "Cool" TableColumn, I would like to replace 对于“ Cool” TableColumn,我想替换为

cellFactory = _ => new TextFieldTableCell[Person, Boolean] (new BooleanStringConverter())

with

val selectedProperty: Int => ObservableValue[Boolean, java.lang.Boolean] = {rowNum: Int => model.characters(rowNum).cool}
cellFactory = column => CheckBoxTableCell.forTableColumn[Person, Boolean](selectedProperty)

in order to get nice CheckBoxes in the TableView (currently, I get TextFields in which I must type out "true" or "false"). 为了在TableView中获得漂亮的CheckBoxes(当前,我得到了TextField,必须在其中键入“ true”或“ false”)。 However, this gives me the (obvious) error: 但是,这给了我(显而易见的)错误:

type mismatch; found : scalafx.beans.property.ObjectProperty[scala.Boolean] required: scalafx.beans.value.ObservableValue[scala.Boolean,java.lang.Boolean]

and if I change 如果我改变

val selectedProperty: Int => ObservableValue[Boolean, java.lang.Boolean] = {rowNum: Int => model.characters(rowNum).cool}

to val selectedProperty = {rowNum: Int => model.characters(rowNum).cool} 到val selectedProperty = {rowNum:Int => model.characters(rowNum).cool}

I get one heck of an error message, which basically comes down to the fact that CheckBoxTableCell.forTableColumn[Person, Boolean](selectedProperty) requires selectedProperty to be of type Int => ObservableValue[Boolean, java.lang.Boolean] , not Int => ObjectProperty[Boolean] 我收到一条错误消息,基本上可以归结为以下事实: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM