我已将 DatePicker 添加到具有以下类的 TableView 中:https ://code.google.com/p/javafx-filterable-table-columns/source/browse/src/main/java/thirdparty/eu/schudt/ javafx/controls/calendar/DatePicker.java?r=db47db237b3342712aafb693f392d403455d5299
我使用 DatePickerCell 类来覆盖 TableCell。DatePicker 在 TableView 中显示良好。但是updateItem方法效果不好,它不会更新ObservableList。
我在这里想念什么?任何人都可以面临同样的问题吗?
这是我的代码:
import java.text.SimpleDateFormat;
import java.util.Locale;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.control.TableCell;
import thirdparty.eu.schudt.javafx.controls.calendar.DatePicker;
public class DatePickerCell<S, T> extends TableCell<S, T> {
private final DatePicker datePicker;
private ObservableValue<T> ov;
public DatePickerCell() {
// Initialize the DatePicker for birthday
this.datePicker = new DatePicker(Locale.ENGLISH);
this.datePicker.setDateFormat(new SimpleDateFormat("dd/MM/yyyy"));
this.datePicker.getCalendarView().todayButtonTextProperty().set("Today");
this.datePicker.getCalendarView().setShowWeeks(false);
this.datePicker.getStylesheets().add("styles/DatePicker.css");
this.setAlignment(Pos.CENTER);
this.setGraphic(this.datePicker);
}
@Override
public void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
setGraphic(this.datePicker);
}
}
以及 FXController 代码中 cellFactory 的部分:
@FXML
private TableColumn revueDateCol;
this.revueDateCol.setCellFactory(new Callback<TableColumn<Revue, Date>, TableCell<Revue, Date>>() {
@Override
public TableCell<Revue, Date> call(TableColumn<Revue, Date> arg0) {
return new DatePickerCell<>();
}
});
感谢您的帮助。
我试图以这种方式实现,但是当我滚动时,单元格返回到原始值。任何想法 ?
public class DatePickerCell<S, T> extends TableCell<Revue, Date> {
private DatePicker datePicker;
public DatePickerCell() {
if (datePicker == null) {
createDatePicker();
}
setGraphic(datePicker);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
Platform.runLater(new Runnable() {
@Override
public void run() {
datePicker.requestFocus();
}
});
}
@Override
public void updateItem(Date item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
if (datePicker != null) {
datePicker.setSelectedDate(getDate());
}
setGraphic(this.datePicker);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
}
private void createDatePicker() {
datePicker = new DatePicker(Locale.FRENCH);
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
datePicker.setDateFormat(formatter);
datePicker.setPromptText("jj/mm/aaaa");
datePicker.getCalendarView().todayButtonTextProperty().set("Aujourd'hui");
datePicker.getCalendarView().setShowWeeks(false);
datePicker.getStylesheets().add("/styles/datePicker.css");
datePicker.selectedDateProperty().addListener(
new ChangeListener<Date>() {
@Override
public void changed(ObservableValue<? extends Date> observable,
Date oldValue, Date newValue) {
commitEdit(newValue);
}
});
setAlignment(Pos.CENTER);
}
private Date getDate() {
return datePicker.getSelectedDate() != null ? datePicker.getSelectedDate() : getItem();
}
@Override
public void startEdit() {
super.startEdit();
}
@Override
public void cancelEdit() {
super.cancelEdit();
setContentDisplay(ContentDisplay.TEXT_ONLY);
}
}
问候,