Who has examples of using cell value factory and cell factory on TableColumn?

1,333 views
Skip to first unread message

Alain FAGOT BÉAREZ

unread,
Feb 20, 2013, 12:51:16 PM2/20/13
to scalaf...@googlegroups.com
Dear all,

In order to close the issue 10 (http://code.google.com/p/scalafx/issues/detail?id=10) before the end of our series of milestones, I would like to add some tests, or at least little demos, for the use of CellValueFactory and CellFactory with TableColumn.

From my current application, I will extract the code for CellFactory with ComboBox, which is kind of a different beast.

Thanks for your contributions,
Alain

Gerhard Lemmer

unread,
Aug 13, 2013, 8:26:55 AM8/13/13
to scalaf...@googlegroups.com
What about an editable table with checkboxes, etc in the TableCell's.
See my question on stackoverflow: http://stackoverflow.com/questions/18207446/editable-boolean-columns-in-scalafx-tableview.

On Monday, February 25, 2013 5:52:53 AM UTC+2, Jarek wrote:
On Wednesday, February 20, 2013 12:51:16 PM UTC-5, Alain FAGOT BÉAREZ wrote:
In order to close the issue 10 (http://code.google.com/p/scalafx/issues/detail?id=10) before the end of our series of milestones, I would like to add some tests, or at least little demos, for the use of CellValueFactory and CellFactory with TableColumn.

There are some examples in https://github.com/jsacha/ProScalaFX/blob/master/src/proscalafx/ch05/ui/StarterAppMain.scala , partially illustrating difficulty with using CellValueFactory from ScalaFX.

Jarek

Jarek

unread,
Aug 13, 2013, 11:21:16 PM8/13/13
to scalaf...@googlegroups.com
On Tuesday, August 13, 2013 8:26:55 AM UTC-4, Gerhard Lemmer wrote:
What about an editable table with checkboxes, etc in the TableCell's.
See my question on stackoverflow: http://stackoverflow.com/questions/18207446/editable-boolean-columns-in-scalafx-tableview.

Here is an example of an editable check box used together with BooleanProperty:

import scalafx.Includes._
import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.beans.property.{BooleanProperty, StringProperty}
import scalafx.collections.ObservableBuffer
import scalafx.scene.Scene
import scalafx.scene.control.TableColumn._
import scalafx.scene.control.cell.{CheckBoxTableCell, TextFieldTableCell}
import scalafx.scene.control.{TableColumn, TableView}

object SimpleEditableTableView extends JFXApp {

  class Person(firstName_ : String, age_ : Int, cool_ : java.lang.Boolean) {
    val name = new StringProperty(this, "Name", firstName_)
    val cool = new BooleanProperty(this, "Cool", cool_)
  }

  val characters = ObservableBuffer[Person](
    new Person("Peggy", 45, false),
    new Person("Rocky", 43, true)
  )

  // Print changes
  characters.zipWithIndex.foreach {
    case (c, i) => c.cool.onChange((_, oldValue, newValue) => println(i + " changed from " + oldValue + " to " + newValue))
  }

  val coolColumn = new TableColumn[Person, java.lang.Boolean] {
      text = "Cool"
      cellValueFactory = _.value.cool.delegate
      prefWidth = 180
  }
  coolColumn.setCellFactory(CheckBoxTableCell.forTableColumn(coolColumn))

  stage = new PrimaryStage {
    title = "Simple Table View"
    scene = new Scene {
      content = new TableView[Person](characters) {
        editable = true
        columns ++= List(
          new TableColumn[Person, String] {
            text = "First Name"
            cellValueFactory = _.value.name
            prefWidth = 180
          },
          coolColumn
        )
      }
    }
  }
}

Gerhard Lemmer

unread,
Aug 24, 2013, 10:09:09 AM8/24/13
to scalaf...@googlegroups.com
Thanks, that did the trick for me. I tried using java.lang.Boolean before, but got stuck on an error in the line `cellValueFactory = {_.value.cool}`. In retrospect it's now obvious I should've used `_.value.cool.delegate`, not `_value.cool`.
Reply all
Reply to author
Forward
0 new messages