javafx TableView Customizing TableCell look depending on item


Sometimes a column should show different content than just the toString value of the cell item. In this case the TableCells created by the cellFactory of the TableColumn is customized to change the layout based on the item.

Important Note: TableView only creates the TableCells that are shown in the UI. The items inside the cells can change and even become empty. The programmer needs to take care to undo any changes to the TableCell that were done when a item was added when it's removed. Otherwise content may still be displayed in a cell where "it doesn't belong".

In the below example setting an item results in the text being set as well as the image displayed in the ImageView:


If the item becomes null or the cell becomes empty, those changes are undone by setting the values back to null:


The following example shows a emoji in addition to text in a TableCell.

The updateItem method is called every time the item of a Cell is changed. Overriding this method allows to react to changes and adjust the look of the cell. Adding a listener to the itemProperty() of a cell would be an alternative, but in many cases TableCell is extended.

Item type

import javafx.scene.image.Image;

// enum providing image and text for certain feelings
public enum Feeling {
    HAPPY("happy", ""),
    SAD("sad", "")
    private final Image emoji;
    private final String value;

    Feeling(String value, String url) {
        // load image in background
        emoji = new Image(url, true);
        this.value = value;

    public Image getEmoji() {
        return emoji;

    public String getValue() {
        return value;

Code in Application class

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;

public class EmotionTable extends Application {

    public static class Item {

        private final ObjectProperty<Feeling> feeling;

        public Item(Feeling feeling) {
            this.feeling = new SimpleObjectProperty<>(feeling);

        public final Feeling getFeeling() {
            return this.feeling.get();

        public final void setFeeling(Feeling value) {

        public final ObjectProperty<Feeling> feelingProperty() {
            return this.feeling;


    public void start(Stage primaryStage) {
        TableView<Item> table = new TableView<>(FXCollections.observableArrayList(
                new Item(Feeling.HAPPY),
                new Item(Feeling.HAPPY),
                new Item(Feeling.HAPPY),
                new Item(Feeling.SAD),
                new Item(Feeling.HAPPY),
                new Item(Feeling.HAPPY),
                new Item(Feeling.SAD)

        EventHandler<ActionEvent> eventHandler = new EventHandler<ActionEvent>() {

            public void handle(ActionEvent event) {
                // change table items depending on userdata of source
                Node source = (Node) event.getSource();
                Feeling targetFeeling = (Feeling) source.getUserData();
                for (Item item : table.getItems()) {
                    if (item != null) {


        TableColumn<Item, Feeling> feelingColumn = new TableColumn<>("Feeling");

        feelingColumn.setCellValueFactory(new PropertyValueFactory<>("feeling"));

        // use custom tablecell to display emoji image
        feelingColumn.setCellFactory(new Callback<TableColumn<Item, Feeling>, TableCell<Item, Feeling>>() {

            public TableCell<Item, Feeling> call(TableColumn<Item, Feeling> param) {
                return new EmojiCell<>();


        Button sunshine = new Button("sunshine");
        Button rain = new Button("rain");



        Scene scene = new Scene(new VBox(10, table, new HBox(10, sunshine, rain)));


    public static void main(String[] args) {


Cell class

import javafx.scene.control.TableCell;
import javafx.scene.image.ImageView;

public class EmojiCell<T> extends TableCell<T, Feeling> {

    private final ImageView image;

    public EmojiCell() {
        // add ImageView as graphic to display it in addition
        // to the text in the cell
        image = new ImageView();


    protected void updateItem(Feeling item, boolean empty) {
        super.updateItem(item, empty);

        if (empty || item == null) {
            // set back to look of empty cell
        } else {
            // set image and text for non-empty cell