我有与JavaFX TableView 文本对齐相同的问题,但旧解决方案不适用于 JavaFX 8。使用下面的代码,单元格被涂成红色,但对齐仍然存在。“-fx-text-alignment”也不起作用。
main.fxml(相关片段)
<AggregatorTableView id="aggregators"/>
样式.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; }
});
}
}