JavaFX TableView文本对齐 [英] JavaFX TableView text alignment
问题描述
>
可以在图片上看到每个colum的文本对齐设置为左对齐。有什么办法改变这个吗?到目前为止,我已经在我的CSS文件尝试:
As you can see on the picture the text alignment for each colum is set to left alignment. Is there any way to change this? So far I've tried within my CSS file:
#Cols{
text-align: right;
}
我也试过:
#Cols{
-fx-text-alignment: right;
}
有谁知道我可以如何改变对齐方式吗?
Does anyone know how I can change the alignment to right?
推荐答案
所有表列的对齐:
从JavaFX-8开始,使用新定义的CSS选择器 table-column
,
#my-table .table-column {
-fx-alignment: CENTER-RIGHT;
}
对于JavaFX-2,
要实现这一点, :
For JavaFX-2, To achieve this define a CSS selector:
#my-table .table-cell {
-fx-alignment: CENTER-RIGHT;
/* The rest is from caspian.css */
-fx-skin: "com.sun.javafx.scene.control.skin.TableCellSkin";
-fx-padding: 0.166667em; /* 2px, plus border adds 1px */
-fx-background-color: transparent;
-fx-border-color: transparent -fx-table-cell-border-color transparent transparent;
-fx-border-width: 0.083333em; /* 1 */
-fx-cell-size: 2.0em; /* 24 */
-fx-text-fill: -fx-text-inner-color;
}
并设置 tableview 的ID。 / p>
and set the id of the tableview.
tableView.setId("my-table");
从JavaFX-8开始,您可以直接将样式应用于 TableColumn
,
Starting from JavaFX-8, you can apply the styling directly to TableColumn
,
firstTextCol.setStyle( "-fx-alignment: CENTER-RIGHT;");
或使用css,
firstTextCol.getStyleClass().add( "custom-align");
其中
.custom-align {
-fx-alignment: center-right;
}
对于JavaFX-2,
应用不同的比对到不同的列,您需要为该列设置单元格工厂。例如,假设表中的第一列应该向左对齐,而其他列使用表的默认对齐方式( CENTER-RIGHT
)。
For JavaFX-2,
To apply different alignments to different columns you need to set cell factory for that column. For instance assume that the 1st column in your table should be aligned to the left while other columns use the table's default alignment (CENTER-RIGHT
in your case).
firstTextCol.setCellFactory(new Callback<TableColumn, TableCell>() {
public TableCell call(TableColumn p) {
TableCell cell = new TableCell<Person, String>() {
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? null : getString());
setGraphic(null);
}
private String getString() {
return getItem() == null ? "" : getItem().toString();
}
};
cell.setStyle("-fx-alignment: CENTER-LEFT;");
return cell;
}
});
这篇关于JavaFX TableView文本对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!