How to use custom cellFactories for TableViews


Question

How does one easily apply custom (reusable) cellFactories to javafx.scene.control.TableCell; or javafx.scene.control.TableView;?

1
5
8/19/2012 2:29:46 PM

Accepted Answer

To apply custom CellFactories which format the displayed text within a cell, its color and so on, the following example might be good help.

Initial situation

Let's assume you have a Bean/POJO Person:

public class Person {
    private double levelOfGrowth = 0;

    public Person() {};

    public Person(double levelOfGrowth) {
        this.levelOfGrowth = levelOfGrowth;
    };

    public double getLevelOfGrowth() {
        return levelOfGrowth;
    }
}

Where levelOfGrowth is the percent-value of how much the growth of a person has been completed.

It might be set between 0.00 to 1.00.

Lets also assume you have created your view inside a FXML which is bound to a controller MainWindowController. You also set the column to display the levelOfGrowth to the id levelOfGrowthColumn

public class MainWindowController implements Initializable {
    @FXML
    public TableColumn levelOfGrowthColumn;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        ObservableList<Person> persons = FXCollections.observableArrayList();

        persons.add(new Person(0));
        persons.add(new Person(0.5));
        persons.add(new Person(1));

        levelOfGrowthColumn.setCellValueFactory(new PropertyValueFactory<Person, Double>("levelOfGrowth"));

    }
}

The intention

The problem in above example is that the values shown in the view are like 0.5 instead of 50%. We also might like to change the color of values like 100% to green.

The solution

What we need is one (reusable) class that describes how the double-values have to be formatted/printed in the view and to connect that class with the levelOfGrowthColumn.

The 'formatter'-class

public class PercantageFormatCell extends TableCell<Object, Double> {

    public PercantageFormatCell() {
    }

    @Override
    protected void updateItem(Double item, boolean empty) {
        super.updateItem(item, empty);

        // If the row is not empty but the Double-value is null,
        // we will always display 0%
        if (!empty && null == item) {
            item = new Double(0.0d);
        }

        // Here we set the displayed text to anything we want without changing the
        // real value behind it. We could also have used switch case or anything you
        // like.
        setText(item == null ? "" : NumberFormat.getPercentInstance().format(item));

        // If the cell is selected, the text will always be white
        // (so that it can be read against the blue background),
        // if the value is 1 it will be green.
        if (item != null) {
            double value = item.doubleValue();
            if (isFocused() || isSelected() || isPressed()) {
                setTextFill(Color.WHITE);
            } else if (value < 1) {
                setTextFill(Color.BLACK);
            } else {
                setTextFill(Color.GREEN);
            }
        }
    }
}

Use it within your controller

public class MainWindowController implements Initializable {
    @FXML
    public TableColumn levelOfGrowthColumn;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        ObservableList<Person> persons = FXCollections.observableArrayList();

        persons.add(new Person(0));
        persons.add(new Person(0.5));
        persons.add(new Person(1));

        // New code START

        // In case we have multiple columns with percent-values it
        // might come in handy to store our formatter
        Callback<TableColumn, TableCell> percantageCellFactory =
            new Callback<TableColumn, TableCell>() {
                public TableCell call(TableColumn p) {
                    return new PercantageFormatCell();
                }
            };

        // Now all we have to do is to apply it
        levelOfGrowthColumn.setCellFactory(percantageCellFactory);

        // New code END

        levelOfGrowthColumn.setCellValueFactory(new PropertyValueFactory<Person, Double>("levelOfGrowth"));

    }
}
13
8/19/2012 2:29:46 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon