editable tableview cell in scalafx with color change

439 views
Skip to first unread message

Erwin Ramírez Millán

unread,
Mar 28, 2015, 10:12:11 AM3/28/15
to scalaf...@googlegroups.com

Hey guys I am new to Scalafx and I am trying to create a tableview that have the ability to be editable each cells and with a change color of a cell when it has been edited, I check the examples and some code for it but still I don't figure out how to do it, I check the javafx examples but what they use is the overriding of the method updateItem in TableCell class, however this method is not implemeted in scalafx :

this is what saids in the Cell.scala class 

// TODO: implement updateItem(T item, boolean empty) // might be difficult since updateItem is a protected method which needs to be defined in the delegate's class

Any help or orientation is more than welcome. Thanks in advance and sorry if is a silly question.

My approach so far is to set a CellFactory to columns as in the javafx but I got stock in the implement updateItem in scalaFX

Jarek Sacha

unread,
Mar 28, 2015, 11:40:44 AM3/28/15
to scalaf...@googlegroups.com

Hello Erwin,

ScalaFX encourages composition over inheritance. In this case, as you mentioned, use CellFactory to customize your table. There is an example of customizing a table using CellFactory, including colors in the cells, in ScalaFX FAQ: Custom cells. Take look and let me know if you need additional information.

Jarek

Erwin Ramírez Millán

unread,
Mar 28, 2015, 4:41:44 PM3/28/15
to scalaf...@googlegroups.com
Hey Jared,

Thank you very much for your reply, I really appreciate it as you have been the only one trying to help me.

In deed I already saw this example but I not what I am looking for as what I want is to change the actual cell of color not to ad an external shape, I saw examples of this but for javafx overriding the updateItem method, as well this change of color is only on Edition of the cell, I don't know how to enable a custom editable cell the only one I know is TextFieldTableCell.forTableColumn() but this one works only with pressing enter, in the tutorial of javafx http://docs.oracle.com/javafx/2/ui_controls/table-view.htm they create a new edition as well overriding the same method updateItem, some idea how to perform this in scala? sorry to bother you, and again thanks.

Jarek Sacha

unread,
Apr 7, 2015, 7:37:24 PM4/7/15
to scalaf...@googlegroups.com

There is no need to overwrite updateItem(), though if you really want to do that it is possible too. First without it. You simply add the code you need to the change handler:

 cellFactory = {
  _ => new TableCell[Person, Color] {
    item.onChange { (_, _, newColor) =>
      graphic = new Circle {fill = newColor; radius = 8}
      // Set cell background color
      style = "-fx-background-color: yellow"
    }
  }
}

If you really want to use updateItem() you will need to extend JavaFX TableCell class rather than ScalaFX class:

cellFactory = {
  _ => new javafx.scene.control.TableCell[Person, Color] {
    override def updateItem(item: Color, empty: Boolean) = {
      super.updateItem(item, empty)
      val g = new Circle {fill = item; radius = 8}
      setGraphic(g)
      setStyle("-fx-background-color: yellow")
    }
  }
}

Overwriting JavaFX classes is not as clean as using ScalaFX API, but possible. Note that within JavaFX class you will need to use JavaFX API to avoid issues, so you have setGraphic(...) instead of graphic = .... Also remember to add import scalafx.Includes._ to avoid some explicit conversions between JavaFX and ScalaFX types.

Jarek

--
You received this message because you are subscribed to the Google Groups "ScalaFX Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalafx-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

scala solist

unread,
Apr 22, 2015, 12:08:56 PM4/22/15
to scalaf...@googlegroups.com
checking empty flag in the updateItem method is crucial. Empty flag does not always correlate with (null == item). If you ignore it, you well get strange behavior on virtual containers. I've managed to do so. And my issues was resolved, when I started to check empty flag. Unfortunately, it requires javafx instead of scalafx.

Jarek Sacha

unread,
Apr 22, 2015, 5:46:55 PM4/22/15
to scalaf...@googlegroups.com
On 4/22/2015 12:08 PM, scala solist wrote:
> checking empty flag in the updateItem method is crucial. Empty flag
> does not always correlate with (null == item). If you ignore it, you
> well get strange behavior on virtual containers. I've managed to do so.

Can you provide some example code to reproduce that issue?

Jarek

J vR

unread,
Feb 6, 2019, 8:38:13 PM2/6/19
to ScalaFX Users
Had the same problem today,
using below seems to be ok:

item.onChange { (_, _, newvalue) =>
  var newStyle = ""
  if (newvalue != "" && newvalue != null && newvalue == "!!!") newStyle = "-fx-background-color: red"
Reply all
Reply to author
Forward
0 new messages