How do I create a ListView with a custom cell?

658 views
Skip to first unread message

Kurt Williams

unread,
Jun 4, 2014, 2:32:52 PM6/4/14
to scalaf...@googlegroups.com
Hi,

I am trying to create a list view that contains an image with a label. JavaFx examples involve creating a custom cell, overriding updateItem() and setting a cell factory. The ScalaFx ListCell does not inherit from the JavaFx ListCell, so I cannot override it.

What is the ScalaFx way to do this?

Thanks.

Kurt

Philippe Jean

unread,
Jun 5, 2014, 4:44:26 AM6/5/14
to scalaf...@googlegroups.com
Take a look here. It's an example for table view but it should be similar for list view.

Jarek Sacha

unread,
Jun 5, 2014, 6:47:13 AM6/5/14
to scalaf...@googlegroups.com
On 6/4/2014 2:32 PM, Kurt Williams wrote:
I am trying to create a list view that contains an image with a label. JavaFx examples involve creating a custom cell, overriding updateItem() and setting a cell factory. The ScalaFx ListCell does not inherit from the JavaFx ListCell, so I cannot override it.

What is the ScalaFx way to do this?

This can be quite easily done using cellFactory. Here is a snippet from TableWithCustomCellDemo  that displays a color circles in table columns:

new TableColumn[Person, Color] {
  text = "Favorite Color"
  // Cell value is loaded from a `Person` object
  cellValueFactory = { _.value.favoriteColor }
  // New circle is created when cell value changes
  // including initial value assignment
  cellFactory = { _ =>
    new TableCell[Person, Color] {
      item.onChange { (_, _, newColor) =>
        graphic = new Circle {
          fill = newColor;
          radius = 8
        }
      }
    }
  }
}


Jarek

Kurt Williams

unread,
Jun 7, 2014, 10:31:10 AM6/7/14
to scalaf...@googlegroups.com

Thanks! 
Reply all
Reply to author
Forward
0 new messages