我非常接近完成这个TableCell。更新:已经过了updateItem()块。我忘了事先调用super.updateItem()。
/**
* A custom DateTimePicker that lets a user select a LocalDateTime.
*
* @author Toni-Tran
*/
public class DateTimePicker extends VBox {
@FXML
private DatePicker datePicker;
@FXML
private TimePicker timePicker;
private ObjectProperty<LocalDateTime> value = new SimpleObjectProperty<>();
private ObjectProperty<EventHandler<ActionEvent>> onAction = new SimpleObjectProperty<>();
//Why not just use a SimpleObjectProperty? Why extend the base?
// private ObjectProperty<EventHandler<ActionEvent>> onAction = new ObjectPropertyBase<EventHandler<ActionEvent>>() {
//
// @Override
// protected void invalidated() {
// super.invalidated();
// setEventHandler(ActionEvent.ACTION, get());
// }
//
//
//
// @Override
// public Object getBean() {
// return this;
// }
//
//
//
// @Override
// public String getName() {
// return "onAction";
// }
// };
public DateTimePicker() {
FXMLLoader loader = new FXMLLoader(this.getClass().getResource(
ScreenPaths.DATE_TIME_PICKER));
loader.setController(this);
loader.setRoot(this);
try {
loader.load();
} catch (IOException e) {
}
}
public DateTimePicker(LocalDateTime ldt) {
super();
value.setValue(ldt);
}
@FXML
private void initialize() {
datePicker.setOnAction(event -> {
value.setValue(LocalDateTime.of(datePicker.getValue(), timePicker.getValue()));
});
value.addListener((ObservableValue<? extends LocalDateTime> observable, LocalDateTime oldValue, LocalDateTime newValue) -> {
fireEvent(new ActionEvent());
});
}
/**
* Manually set a LocalDateTime value for this control.
*
* @param val The {@link LocalDateTime} value for this control.
*/
public void setValue(LocalDateTime val) {
if (val == null) {
return;
}
datePicker.setValue(val.toLocalDate());
timePicker.setValue(val.toLocalTime());
}
/**
* Returns the currently set LocalDateTime.
*
* @return the {@link LocalDateTime} value of this control.
*/
public LocalDateTime getValue() {
return value.getValue();
}
/**
* Returns the Observable property of the DateTimePicker's current
* LocalDateTime value.
*
* @return the Observable property of the DateTimePicker's current
* LocalDateTime value.
*/
public ObjectProperty<LocalDateTime> valueProperty() {
return value;
}
public final ObjectProperty<EventHandler<ActionEvent>> onActionProperty() {
return onAction;
}
public final EventHandler<ActionEvent> getOnAction() {
return onActionProperty().get();
}
public final void setOnAction(EventHandler<ActionEvent> value) {
onActionProperty().set(value);
}
}
但问题是,当调用super.commitEdit()时,检索的项是null?我认为问题出在我的DateTimePicker类中。在更改的值上,它返回null而不是新值。