0

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"/>

styles.css

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

AggregatorTableView.java

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.setCellFactory(CheckBoxTableCell.forTableColumn(activeCol));
        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);
        setEditable(true);
        setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
        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 "test.org"; }
            @Override public List<DataItem> receiveData() { return null; }
            @Override public BooleanProperty isActive() { return isActive; }
        });
    }
}
Community
  • 1
  • 1
aemxdp
  • 1,348
  • 1
  • 14
  • 34
  • 1
    I face similar issues: http://stackoverflow.com/questions/19918636/tablecell-not-applied-consistently. Have posted on jira: https://javafx-jira.kenai.com/browse/RT-33372?focusedCommentId=368263&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-368263 in the comments. – assylias Nov 12 '13 at 16:26

2 Answers2

1

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:

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

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.

Community
  • 1
  • 1
bashoogzaad
  • 4,611
  • 8
  • 40
  • 65
0

Here's a snippet of code doing the same alignment:

    TableColumn sizeCol = new TableColumn("Size");
    sizeCol.setResizable(true);
    sizeCol.setStyle("-fx-alignment: CENTER-RIGHT;");
    sizeCol.setCellValueFactory(new PropertyValueFactory<Parameter, Long>("size"));

It worked for me, but I read an old post where someone said it didn't work for editing. I didn't test that out since my table is read only.

Tom Rutchik
  • 1,183
  • 1
  • 12
  • 15