Мне нужно сделать ячейку в TableView редактируемой и выделить ее жирным шрифтом после фиксации изменений. Я могу сделать эти вещи по отдельности, но не вместе, так как они реализуются через метод setCellFactory.
Вот части кода:
Настройка клеточной фабрики
((TableColumn) lpAttributesTable.getColumns().get(1)).setCellFactory(
new Callback<TableColumn<LPAttributesTableObject,String>, TableCell<LPAttributesTableObject,String>>() {
@Override
public TableCell<LPAttributesTableObject,String> call(
TableColumn<LPAttributesTableObject,String> param) {
TextFieldTableCell<LPAttributesTableObject, String> editableTableCell =
new TextFieldTableCell<LPAttributesTableObject, String>(new StringConverter<String>() {
@Override
public String toString(String object) {
return object;
}
@Override
public String fromString(String string) {
return string;
}
})
{
@Override
public void updateItem(String item, boolean empty)
{
super.updateItem(item,empty);
if (item == null || empty) {
setText(null);
// setStyle("");
}
else
{
setText(item);
styleProperty().bind(
Bindings.when(getTableRow().selectedProperty()).
then("-f-font-weight:bold;").otherwise(""));
}
}
};
return editableTableCell;
}
});
LPAttributesTableObject
public class LPAttributesTableObject {
private String attribute;
private String value;
public LPAttributesTableObject(String _attribute, String _value)
{
this.attribute = _attribute;
this.value = _value;
}
public final String getAttribute() { return attribute; }
public final String getValue() { return value; }
public StringProperty attributeProperty() { return new SimpleStringProperty(attribute); }
public StringProperty valueProperty() { return new SimpleStringProperty(value); }
public final void setAttribute(String _attr) { this.attribute = _attr;}
public final void setValue(String _description) { this.value = _description;}
}
Таблица с модельной привязкой. Обратите внимание, я закомментировал CellFactory только для редактирования.
((TableColumn) lpattrsTable.getColumns().get(1)).setCellValueFactory(new PropertyValueFactory<LPAttributesTableObject,String>("value"));
// ((TableColumn) lpattrsTable.getColumns().get(1)).setCellFactory(TextFieldTableCell.forTableColumn(new DefaultStringConverter()));
Текущая реализация позволяет только редактирование ячеек.
Setting cellFactory
попробуйте исправить опечатку вBindings.when...("-f-font-weight:bold;").otherwise(""));
с помощьюBindings.when...("-fx-font-weight:bold;").otherwise(""));
. - person iMan   schedule 25.08.2016