我在以下列的所有行中创建一个TableCellTextField:
nameColumn.setCellFactory(TextFieldTableCell.forTableColumn());
我的目的是根据用户的输入更改已编辑的单元格的背景颜色。
我找到了几种改变单个单元格背景颜色的解决方案,它们包括覆盖单元工厂内的updateItem()方法。 Here就是一个例子。
但是,我不知道如何将该策略与通过TextField编辑单元格结合起来。
答案 0 :(得分:2)
您可以使用TextFieldTableCell
基本上对常规TableCell
执行相同的操作。请注意,与普通TableCell
不同,TextFieldTableCell
已经负责设置文本等。
所以你可以这样做,例如:
PseudoClass specialClass = PseudoClass.getPseudoClass("special");
nameColumn.setCellFactory(tc -> new TextFieldTableCell<MyType, String>(TextFormatter.IDENTITY_STRING_CONVERTER) {
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
boolean condition = /* depends on item and empty.... */
pseudoClassStateChanged(specialClass, condition);
}
});
然后在CSS文件中,只需定义单元格所需的样式。 E.g。
.table-cell:special {
-fx-background-color: yellow ;
}