atjua
atjua

Reputation: 539

how to create an updateable tableview cell in Scala

I have created a tableview with their components inside it, assigned cellValueFactory and have set the properties editable to true. Somewhere in my code, I have the following :

    ...
    tableID.selectionModel().selectedItem.onChange(
          (_, _, newValue) => col_uname.setCellFactory(TextFieldTableCell.forTableColumn());
    ...

With it, I managed to create to convert it to textfield and are allowed to type in it. However,after finishing typing, the text reversed back to previous text before the edit. What type/piece of code should I include make sure that the text is updated properly? I've tried searching on google, but there's no explanation for it so far.

Upvotes: 2

Views: 920

Answers (1)

Jarek
Jarek

Reputation: 1533

You should be able to edit table by, as you mentioned, editable = true and adding cell factory with a text field, for instance:

new TableColumn[Person, String] {
  text = "First Name"
  cellValueFactory = {_.value.firstName}
  cellFactory = TextFieldTableCell.forTableColumn()
  prefWidth = 180
}

The JavaFX Table View Tutorial also suggests using OnEditCommit. Not sure if that is really necessary. Here is a complete example that works without using OnEditCommit:

import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.beans.property.StringProperty
import scalafx.collections.ObservableBuffer
import scalafx.event.ActionEvent
import scalafx.scene.Scene
import scalafx.scene.control.TableColumn._
import scalafx.scene.control.cell.TextFieldTableCell
import scalafx.scene.control.{Button, TableColumn, TableView}
import scalafx.scene.layout.VBox

object EditableTableView extends JFXApp {

  class Person(firstName_ : String, lastName_ : String) {

    val firstName = new StringProperty(this, "firstName", firstName_)
    val lastName  = new StringProperty(this, "lastName", lastName_)

    firstName.onChange { (_, oldValue, newValue) => println(s"Value changed from `$oldValue` to `$newValue`") }
    lastName.onChange { (_, oldValue, newValue) => println(s"Value changed from `$oldValue` to `$newValue`") }
    override def toString = firstName() + " " + lastName()
  }

  val characters = ObservableBuffer[Person](
    new Person("Peggy", "Sue"),
    new Person("Rocky", "Raccoon")
  )

  stage = new PrimaryStage {
    title = "Editable Table View"
    scene = new Scene {
      root = new VBox {
        children = Seq(
          new TableView[Person](characters) {
            editable = true
            columns ++= List(
              new TableColumn[Person, String] {
                text = "First Name"
                cellValueFactory = {_.value.firstName}
                cellFactory = TextFieldTableCell.forTableColumn()
                prefWidth = 180
              },
              new TableColumn[Person, String]() {
                text = "Last Name"
                cellValueFactory = {_.value.lastName}
                cellFactory = TextFieldTableCell.forTableColumn()
                prefWidth = 180
              }
            )
          },
          new Button {
            text = "Print content"
            onAction = (ae: ActionEvent) => {
              println("Characters:")
              characters.foreach(println)
            }
          }
        )
      }
    }
  }
}

Upvotes: 1

Related Questions