Reputation: 1339
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; }
});
}
}
Upvotes: 0
Views: 10204
Reputation: 1692
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.
Upvotes: 0
Reputation: 4781
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.
Upvotes: 1