问题描述
我已经实现了一个自定义的datePickerTableCell来显示单元格编辑上的日期选择器。我正在使用ExtFX DatePicker() 。一切正常,除非我手动输入日期而不是从选择器中选择日期,我在commitEdit()上得到一个NullPointerException。
I have implemented a custom datePickerTableCell to show a date picker on cell edit. I am using the ExtFX DatePicker (https://bitbucket.org/sco0ter/extfx/overview). Everything works fine except when I enter the date manually instead of choosing it from picker, I get a NullPointerException on commitEdit().
datePicker.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent t) {
if (t.getCode() == KeyCode.ENTER) {
if(getItem() == null) {
commitEdit(null);
} else {
commitEdit(getItem());
}
} else if (t.getCode() == KeyCode.ESCAPE) {
cancelEdit();
}
}
});
我发现这里出了什么问题。
I coudnt find whats going wrong here.
推荐答案
也许看看这个例子。它使用颜色选择器,但也适用于日期选择器:
Maybe have a look at this example. It uses a color picker but works as well for a date picker:
public class ColorTableCell<T> extends TableCell<T, Color> {
private final ColorPicker colorPicker;
public ColorTableCell(TableColumn<T, Color> column) {
this.colorPicker = new ColorPicker();
this.colorPicker.editableProperty().bind(column.editableProperty());
this.colorPicker.disableProperty().bind(column.editableProperty().not());
this.colorPicker.setOnShowing(event -> {
final TableView<T> tableView = getTableView();
tableView.getSelectionModel().select(getTableRow().getIndex());
tableView.edit(tableView.getSelectionModel().getSelectedIndex(), column);
});
this.colorPicker.valueProperty().addListener((observable, oldValue, newValue) -> {
if(isEditing()) {
commitEdit(newValue);
}
});
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
@Override
protected void updateItem(Color item, boolean empty) {
super.updateItem(item, empty);
setText(null);
if(empty) {
setGraphic(null);
} else {
this.colorPicker.setValue(item);
this.setGraphic(this.colorPicker);
}
}
}
如果您使用的是Java 7,用匿名内部类替换lambdas,但它也应该工作。完整的博客文章是这里。
If you're on Java 7, replace the lambdas with anonymous inner classes, but it should work as well. Full blog post is here.
这篇关于Javafx TableCell中的DatePicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!