在 JavaFX TableView 中,我怎样才能
- 创建多行列?
- 将其内容居中?
- 并为每一(整)行设置背景颜色?
我设法使用自定义 CellFactory
创建了一个多行列。我也知道 setAlignment(Pos.CENTER)
和 setTextAlignment(TextAlignment.CENTER)
来居中文本。但是,我的示例应用程序中的文本每行都没有正确居中。此外,我没有设法在 Text
对象上设置背景颜色。现在我的方法是为每一行添加一个 Pane
,效果很好。但是如何让 Pane
填满列的整个宽度和高度的 1/3?
作为起点,这就是我期望代码的样子(不过,我知道它没有按照我的意愿行事):
multiCol.setCellFactory(new Callback<TableColumn<Person, Person>, TableCell<Person, Person>>() {
@Override public TableCell<Person, Person> call(TableColumn<Person, Person> multiCol) {
return new TableCell<Person, Person>() {
private Group grp = null;
@Override public void updateItem(final Person person, boolean empty) {
super.updateItem(person, empty);
this.setAlignment(Pos.CENTER);
if (!isEmpty()) {
Text text = new Text(person.getFirstName());
text.setX(0);
text.setY(0);
text.setTextAlignment(TextAlignment.CENTER); // Center text?
Pane pane = new Pane();
pane.setStyle("-fx-background-color: #66BB66;");
pane.setLayoutX(0);
pane.setLayoutY(0);
pane.setPrefHeight(20);
pane.setPrefWidth(this.prefWidth(-1)); // Column width?
// -----
Text text2 = new Text(person.getLastName());
text2.setX(0);
text2.setY(20);
text2.setTextAlignment(TextAlignment.CENTER); // Center text?
Pane pane2 = new Pane();
pane2.setStyle("-fx-background-color: #79A8D8;");
pane2.setLayoutX(0);
pane2.setLayoutY(20);
pane2.setPrefHeight(20);
pane2.setPrefWidth(this.prefWidth(-1)); // Column width?
// -----
Text text3 = new Text(person.getEmail());
text3.setX(0);
text3.setY(40);
text3.setTextAlignment(TextAlignment.CENTER); // Center text?
Pane pane3 = new Pane();
pane3.setStyle("-fx-background-color: #FF8888;");
pane3.setLayoutX(0);
pane3.setLayoutY(40);
pane3.setPrefHeight(20);
pane3.setPrefWidth(this.prefWidth(-1)); // Column width?
// -----
Group grp = new Group();
grp.getChildren().add(pane);
grp.getChildren().add(text);
grp.getChildren().add(pane2);
grp.getChildren().add(text2);
grp.getChildren().add(pane3);
grp.getChildren().add(text3);
setGraphic(grp);
setStyle("-fx-padding: 0 0 0 0;");
}
}
};
}
});
我期待这样的输出:
有关完整的可编译代码示例,请查看 this pastebin .
最佳答案
使用合适的布局面板(例如 VBox
),并向其添加 Label
。您可以使用 VBox.setHgrow(...)
配置标签以填充 VBox
的宽度。您还需要设置标签的最大宽度以允许其增长。
顺便说一句,每次调用 updateItem(...)
方法时都重新创建控件并不是好的做法。创建它们一次,然后只需在 updateItem(...)
方法中使用所需数据配置它们。
例子:
TableColumn<Person, Person> multiCol = new TableColumn<>("Multiline");
multiCol.setCellValueFactory(cellData ->
new ReadOnlyObjectWrapper<Person>(cellData.getValue()));
multiCol.setCellFactory(column -> new TableCell<Person, Person>() {
private VBox graphic ;
private Label firstNameLabel ;
private Label lastNameLabel ;
private Label emailLabel ;
// Anonymous constructor:
{
graphic = new VBox();
firstNameLabel = createLabel("#66BB66");
lastNameLabel = createLabel("#79A8D8");
emailLabel = createLabel("#FF8888");
graphic.getChildren().addAll(firstNameLabel,
lastNameLabel, emailLabel);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
private final Label createLabel(String color) {
Label label = new Label();
VBox.setVgrow(label, Priority.ALWAYS);
label.setMaxWidth(Double.MAX_VALUE);
label.setStyle("-fx-background-color: "+color+" ;");
label.setAlignment(Pos.CENTER);
return label ;
}
@Override
public void updateItem(Person person, boolean empty) {
if (person == null) {
setGraphic(null);
} else {
firstNameLabel.setText(person.getFirstName());
lastNameLabel.setText(person.getLastName());
emailLabel.setText(person.getEmail());
setGraphic(graphic);
}
}
});
关于具有多行、背景颜色和居中文本的 JavaFX 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26844275/