JavaFX中TextFieldTableCell的默认行为是:
当焦点丢失时取消编辑非常不自然,导致我的用户丢失数据。
我已经尝试创建TextFieldTableCell的替代实现,我在textfield.focusedProperty()
添加了一个监听器以强制提交,但这根本不起作用。
是否有人建议如何改进此行为?
我的改编createTextField
- 功能:
private static <T> TextField createTextField(final Cell<T> cell, final StringConverter<T> converter) {
final TextField textField = new TextField(getItemText(cell, converter));
// Use onAction here rather than onKeyReleased (with check for Enter),
// as otherwise we encounter RT-34685
textField.setOnAction(event -> {
if (converter == null) {
throw new IllegalStateException(
"Attempting to convert text input into Object, but provided "
+ "StringConverter is null. Be sure to set a StringConverter "
+ "in your cell factory.");
}
cell.commitEdit(converter.fromString(textField.getText()));
event.consume();
});
textField.setOnKeyReleased(t -> {
if (t.getCode() == KeyCode.ESCAPE) {
cell.cancelEdit();
t.consume();
}
});
textField.focusedProperty().addListener(observable -> {
if (!textField.isFocused()) {
cell.commitEdit(converter.fromString(textField.getText()));
}
});
return textField;
}