【发布时间】:2021-08-07 08:48:15
【问题描述】:
我有一个简单的 TableView,有两列(Id,Valid)。
表格代码如下:
private TableView<List<Object>> createTable()
{
TableView<List<Object>> table = new TableView<>();
TableColumn<List<Object>, String> idCol = new TableColumn<>("ID");
idCol.setCellValueFactory(new Callback<CellDataFeatures<List<Object>, String>, ObservableValue<String>>()
{
public ObservableValue<String> call(CellDataFeatures<List<Object>, String> param)
{
return new SimpleStringProperty(param.getValue().get(0).toString());
}
});
TableColumn<List<Object>, String> validCol = new TableColumn<>("Valid");
validCol.setCellValueFactory(new Callback<CellDataFeatures<List<Object>, String>, ObservableValue<String>>()
{
public ObservableValue<String> call(CellDataFeatures<List<Object>, String> param)
{
return new SimpleStringProperty(param.getValue().get(1).toString());
}
});
table.getColumns().addAll(idCol, validCol);
List<Object> firstRow = new ArrayList<Object>();
firstRow.add(1);
firstRow.add(0);
List<Object> secondRow = new ArrayList<Object>();
secondRow.add(2);
secondRow.add(1);
table.getItems().addAll(firstRow, secondRow);
return table;
}
有效行的背景应变为绿色,无效行的背景应变为红色。我通过设置行工厂意识到这一点:
table.setRowFactory(new Callback<TableView<List<Object>>, TableRow<List<Object>>>()
{
@Override
public TableRow<List<Object>> call(TableView<List<Object>> param)
{
return new TableRow<List<Object>>()
{
@Override
protected void updateItem(List<Object> item, boolean empty)
{
super.updateItem(item, empty);
if (!empty)
{
int valid = Integer.parseInt(item.get(1).toString());
if (valid == 1)
setBackground(MyCustomBackGround.VALID);
else if (valid == 0)
setBackground(MyCustomBackGround.NOT_VALID);
else
setBackground(MyCustomBackGround.WHITE);
}
}
};
}
});
这似乎工作正常。但现在我想改变我的选择的颜色。可以说我想使设置的颜色变暗。
我怎么能意识到这一点?
【问题讨论】: