Reputation: 157
I am creating a TableCellTextField in all rows in the following column:
nameColumn.setCellFactory(TextFieldTableCell.forTableColumn());
My intention is to change the edited cell's background color depending on the user's input.
I have found several solutions for changing a single cell's background color which consist in overriding the updateItem() method inside the cell factory. Here is an example.
However, I do not know how to combine that strategy with making the cell editable through a TextField.
Upvotes: 0
Views: 514
Reputation: 209694
You can basically do the same thing with a TextFieldTableCell
that you do with a regular TableCell
. Note that, unlike a plain TableCell
, a TextFieldTableCell
already takes care of setting the text etc.
So you can do, e.g.:
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);
}
});
Then in your CSS file, just define the styles you need for the cell. E.g.
.table-cell:special {
-fx-background-color: yellow ;
}
Upvotes: 2