有没有办法为独立的TableView列设置样式? [英] Is there a way to style an independent TableView column?
本文介绍了有没有办法为独立的TableView列设置样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以使用CSS来设置单元格的样式,但是如果我想要一个不同的样式(比如使用不同的文本颜色),只需要一列。
I can use the CSS to style cells, but what if I want a different style (like using a different text color) for just one column.
也许我我错过了什么。
推荐答案
你应该使用 TableColumn #setCellFactory()来自定义单元格项呈现。
例如,像这样的数据模型人员类 :
You should to use TableColumn#setCellFactory() to customize cell item rendering.
For example, datamodel with like this Person class:
// init code vs..
TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
firstNameCol.setCellFactory(getCustomCellFactory("green"));
TableColumn lastNameCol = new TableColumn("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
lastNameCol.setCellFactory(getCustomCellFactory("red"));
table.setItems(data);
table.getColumns().addAll(firstNameCol, lastNameCol);
// scene create code vs..
和普通 getCustomCellFactory()
方法:
private Callback<TableColumn<Person, String>, TableCell<Person, String>> getCustomCellFactory(final String color) {
return new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
@Override
public TableCell<Person, String> call(TableColumn<Person, String> param) {
TableCell<Person, String> cell = new TableCell<Person, String>() {
@Override
public void updateItem(final String item, boolean empty) {
if (item != null) {
setText(item);
setStyle("-fx-text-fill: " + color + ";");
}
}
};
return cell;
}
};
}
这篇关于有没有办法为独立的TableView列设置样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文