JavaFX 8 TableView Text Alignment


I have the same problem as in JavaFX TableView text alignment, but old solution doesn't work with JavaFX 8. With code below, the cells are painted red, but alignment is still left. "-fx-text-alignment" doesn't work either.

main.fxml (relevant fragment)

<AggregatorTableView id="aggregators"/>


#aggregators .table-cell {
    -fx-alignment: CENTER-RIGHT;
    -fx-background-color: red;

public class AggregatorTableView extends TableView<Aggregator> {
    public AggregatorTableView() {
        TableColumn<Aggregator, Boolean> activeCol = new TableColumn<>("Active?");
        TableColumn<Aggregator, String> nameCol = new TableColumn<>("Name");
        TableColumn<Aggregator, String> resourceCol = new TableColumn<>("Resource");
        TableColumn<Aggregator, String> versionCol = new TableColumn<>("Version");
        activeCol.setCellValueFactory(cdf -> cdf.getValue().isActive());
        activeCol.setOnEditCommit(e -> e.getRowValue().isActive().set(e.getNewValue()));
        nameCol.setCellValueFactory(cdf -> new SimpleStringProperty(cdf.getValue().getName()));
        resourceCol.setCellValueFactory(cdf -> new SimpleStringProperty(cdf.getValue().getResource()));
        versionCol.setCellValueFactory(cdf -> new SimpleStringProperty(cdf.getValue().getVersion()));
        getColumns().addAll(activeCol, nameCol, resourceCol, versionCol);
        getItems().add(new Aggregator() {
            private BooleanProperty isActive = new SimpleBooleanProperty(true);
            @Override public String getName() { return "test"; }
            @Override public String getVersion() { return "0.0.1"; }
            @Override public String getResource() { return ""; }
            @Override public List<DataItem> receiveData() { return null; }
            @Override public BooleanProperty isActive() { return isActive; }
5/23/2017 11:50:08 AM

The solution mentioned by @UlukBiy (here: JavaFX TableView text alignment) works for my, and I am using javaFX8.

I just defined my Tableview in FXML like this:

    <TableColumn fx:id="couponHash" text="Kortingscode">
    <cellValueFactory><PropertyValueFactory property="hashID" /></cellValueFactory>
    <TableColumn fx:id="couponTimeCreated" text="Aangemaakt op">
    <cellValueFactory><PropertyValueFactory property="timestampCreated" /></cellValueFactory>

The FXML file is connected to a css file, where I only inserted this style class:

.table-cell {
    -fx-alignment: CENTER-RIGHT;

This works for me with JavaFX8. Please let me know if it works for you.

5/23/2017 10:25:58 AM

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