JavaFX设置TableColumn的单元格背景色
我用CSS文件设置了TableColumn,我想给每个单元格提供不同的背景颜色。我怎样才能做到这一点 示例)表格第1~5列JavaFX设置TableColumn的单元格背景色,javafx,background,tableview,cell,tablecell,Javafx,Background,Tableview,Cell,Tablecell,我用CSS文件设置了TableColumn,我想给每个单元格提供不同的背景颜色。我怎样才能做到这一点 示例)表格第1~5列 表第1列第3行为黑色和黑色 表4第4行第5列为绿色。。。etc要更改表格视图中特定单元格的行为,您需要设置表格列的单元格工厂。下面是一个例子: TableColumn tc = new TableColumn(); tc.getStyleClass.add(".style in css file") 导入javafx.application.application; 导
表第1列第3行为黑色和黑色
表4第4行第5列为绿色。。。etc要更改
表格视图中特定单元格的行为
,您需要设置表格列的单元格工厂
。下面是一个例子:
TableColumn tc = new TableColumn();
tc.getStyleClass.add(".style in css file")
导入javafx.application.application;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.scene.scene;
导入javafx.scene.control.TableCell;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.stage.stage;
公共类TableMCVE扩展了应用程序{
@凌驾
公众假期开始(阶段){
ObservableList tableData=FXCollections.observableArrayList();
tableData.add(FXCollections.observableArrayList(“Row1Col1”、“Row1Col2”));
tableData.add(FXCollections.observableArrayList(“Row2Col1”、“Row2Col2”));
tableData.add(FXCollections.observableArrayList(“Row3Col1”、“Row3Col2”));
TableView table=新TableView();
TableColumn col1=新的TableColumn(“col1”);
col1.setCellValueFactory(e->new SimpleStringProperty(e.getValue().get(0));
//使用自定义TableCell设置列的单元格工厂以修改其行为。
col1.setCellFactory(e->new TableCell(){
@凌驾
public void updateItem(字符串项,布尔值为空){
//始终调用超级构造函数。
super.updateItem(项,空);
如果(项==null | |空){
setText(空);
}否则{
setText(项目);
//如果索引为2,则显式设置背景色。
如果(getIndex()==2){
此.setStyle(“-fx背景色:绿色;”);
}
}
}
});
TableColumn col2=新的TableColumn(“col2”);
col2.setCellValueFactory(e->new SimpleStringProperty(e.getValue().get(1));
//使用自定义TableCell设置列的单元格工厂以修改其行为。
col2.setCellFactory(e->new TableCell(){
@凌驾
public void updateItem(字符串项,布尔值为空){
//始终调用超级构造函数。
super.updateItem(项,空);
如果(项==null | |空){
setText(空);
}否则{
setText(项目);
//如果索引为零,则显式设置背景色。
如果(getIndex()==0){
此.setStyle(“-fx背景色:蓝色;”);
}
}
}
});
table.getColumns().addAll(col1,col2);
table.getItems().addAll(tableData);
舞台场景(新场景(表格));
stage.show();
}
公共静态void main(字符串[]args){
发射();
}
}
在我的示例中,我只是根据单元格的索引设置颜色,但您可以将其更改为更有意义的颜色。例如,颜色可以基于单元格的值。要更改表格视图中特定单元格的行为
,您需要设置表格列的单元格工厂
。下面是一个例子:
TableColumn tc = new TableColumn();
tc.getStyleClass.add(".style in css file")
导入javafx.application.application;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.scene.scene;
导入javafx.scene.control.TableCell;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.stage.stage;
公共类TableMCVE扩展了应用程序{
@凌驾
公众假期开始(阶段){
ObservableList tableData=FXCollections.observableArrayList();
tableData.add(FXCollections.observableArrayList(“Row1Col1”、“Row1Col2”));
tableData.add(FXCollections.observableArrayList(“Row2Col1”、“Row2Col2”));
tableData.add(FXCollections.observableArrayList(“Row3Col1”、“Row3Col2”));
TableView table=新TableView();
TableColumn col1=新的TableColumn(“col1”);
col1.setCellValueFactory(e->new SimpleStringProperty(e.getValue().get(0));
//使用自定义TableCell设置列的单元格工厂以修改其行为。
col1.setCellFactory(e->new TableCell(){
@凌驾
public void updateItem(字符串项,布尔值为空){
//始终调用超级构造函数。
super.updateItem(项,空);
如果(项==null | |空){
setText(空);
}否则{
setText(项目);
//如果索引为2,则显式设置背景色。
如果(getIndex()==2){
此.setStyle(“-fx背景色:绿色;”);
}
}
}
});
TableColumn col2=新的TableColumn(“col2”);
col2.setCellValueFactory(e->new SimpleStringProperty(e.getValue().get(1));
//使用自定义TableCell设置列的单元格工厂以修改其行为。
col2.setCellFactory(e->new TableCell(){
@凌驾
public void updateItem(字符串项,布尔值为空){
//始终调用超级构造函数。
super.updateItem(项,空);
如果(项==null | |空){
setText(空);
}否则{
setText(项目);
//如果索引为零,则设置背景色
TableView<Item<String>> tableView = new TableView<>();
// sample item class contains a single property with the same type as the type parameter (String in this case)
tableView.getItems().addAll(
new Item<>("1"),
new Item<>("2"),
new Item<>("4"),
new Item<>("5"),
new Item<>("6"),
new Item<>("7"),
new Item<>("8"),
new Item<>("9")
);
// create columns
TableColumn<Item<String>, String> column1 = new TableColumn<>("value");
TableColumn<Item<String>, Void> column2 = new TableColumn<>();
tableView.getColumns().addAll(column1, column2);
// create list of colors (CSS)
final List<String> colors = Arrays.asList(
"blue",
"green",
"red",
"violet",
"yellow",
...
);
Callback factory = new Callback<TableColumn<Item<String>, Object>, TableCell<Item<String>, Object>>() {
private int columns = tableView.getColumns().size();
@Override
public TableCell<Item<String>, Object> call(TableColumn<Item<String>, Object> param) {
return new TableCell<Item<String>, Object>() {
private int columnIndex = param.getTableView().getColumns().indexOf(param);
@Override
public void updateIndex(int i) {
super.updateIndex(i);
// select color based on index of row/column
if (i >= 0) {
// select color repeating the color, if we run out of colors
String color = colors.get((i * columns + columnIndex) % colors.size());
this.setStyle("-fx-my-cell-background: " + color + ";");
System.out.println(getStyle());
}
}
@Override
protected void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
// assign item's toString value as text
if (empty || item == null) {
setText(null);
} else {
setText(item.toString());
}
}
};
}
};
column1.setCellValueFactory(new PropertyValueFactory<>("value"));
column1.setCellFactory(factory);
column2.setCellFactory(factory);
Scene scene = new Scene(tableView);
scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());