我正在使用DatePickerCell类在TableView单元格中创建Datepicker,但我无法保留其值。
这是用于呈现单元格的DatePickerCell类,它是从带有生日示例的论坛中获取的。
public class DatePickerCell<S, T> extends TableCell<T, Date> {
private DatePicker datePicker;
public DatePickerCell() {
super();
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);
SimpleDateFormat smp = new SimpleDateFormat("dd/MM/yyyy");
if (null == this.datePicker) {
System.out.println("datePicker is NULL");
}
if (empty) {
setText(null);
setGraphic(null);
} else {
if (isEditing()) {
setContentDisplay(ContentDisplay.TEXT_ONLY);
} else {
setDatepikerDate(smp.format(new Date()));
setText(smp.format(new Date()));
setGraphic(this.datePicker);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
}
}
private void setDatepikerDate(String dateAsStr) {
LocalDate ld = null;
int day, month, year;
day = month = year = 0;
try {
day = Integer.parseInt(dateAsStr.substring(0, 2));
month = Integer.parseInt(dateAsStr.substring(3, 5));
year = Integer.parseInt(dateAsStr.substring(6, dateAsStr.length()));
} catch (NumberFormatException e) {
System.out.println("setDatepikerDate / unexpected error " + e);
}
ld = LocalDate.of(year, month, day);
datePicker.setValue(ld);
}
private void createDatePicker() {
this.datePicker = new DatePicker();
datePicker.setPromptText("dd/MM/yyyy");
datePicker.setEditable(true);
datePicker.setOnAction(t -> {
LocalDate date = datePicker.getValue();
int index1 = getIndex();
SimpleDateFormat smp = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, date.getDayOfMonth());
cal.set(Calendar.MONTH, date.getMonthValue() - 1);
cal.set(Calendar.YEAR, date.getYear());
setText(smp.format(cal.getTime()));
commitEdit(cal.getTime());
});
setAlignment(Pos.CENTER);
}
@Override
public void startEdit() {
super.startEdit();
}
@Override
public void cancelEdit() {
super.cancelEdit();
setContentDisplay(ContentDisplay.TEXT_ONLY);
}
public DatePicker getDatePicker() {
return datePicker;
}
public void setDatePicker(DatePicker datePicker) {
this.datePicker = datePicker;
}
}
我想知道的是,当我在表中使用它并尝试添加一行或多行时,先前行中先前选择的日期设置为默认(系统日期)日期,并且我选择的值已消失。我无法理解为什么重新初始化所有日期选择器或重置日期。
这就是我在我的代码中使用它的方式
@FXML private TableView<Stock> tblStock;
@FXML private TableColumn<Stock, Date> colDate;
colDate.setCellValueFactory(new PropertyValueFactory<>("date1"));
colDate.setCellFactory(cell -> new DatePickerCell());
在我的Stock类中,这就是我初始化date1
的方式private Date date1 = new Date();
答案 0 :(得分:1)
最后,经过长时间的搜索,我才能实现目标。感谢这个person,我得到了一个带有datepicker的工作表单元格。
以下是未来参考的代码:
DatePickerCell类
private SimpleObjectProperty<Date> date = new SimpleObjectProperty<>(/*your parameter*/);
//getter
public Date getDate() {
return date.get();
}
//property getter
public SimpleObjectProperty<Date> dateProperty() {
return date;
}
//setter
public void setDate(Date date) {
this.date.set(date);
}
在应用中使用
模型类
@FXML private TableColumn<Stock, Date> colDate;
//because using FXML document, initialization not needed
colDate.setCellValueFactory(cell -> cell.getValue().dateProperty());
colDate.setCellFactory(cell -> new DatePickerCell<Stock, Date>());
colDate.setOnEditCommit(event -> event.getTableView().getItems()
.get(event.getTablePosition().getRow()).setDate(event.getNewValue()));
控制器的
<?php
function method_1(Array &$arr) {
return $arr === array_values($arr);
}
function method_2(Array &$arr) {
for (reset($arr), $i = 0; key($arr) !== $i++; next($arr));
return is_null(key($arr));
}
function method_3(Array &$arr) {
return array_keys($arr) === range(0, count($arr) - 1);
}
function method_4(Array &$arr) {
$idx = 0;
foreach( $arr as $key => $val ){
if( $key !== $idx )
return FALSE;
$idx++;
}
return TRUE;
}
function benchmark(Array $methods, Array &$target){
foreach($methods as $method){
$start = microtime(true);
for ($i = 0; $i < 1000; $i++)
$dummy = call_user_func($method, $target);
$end = microtime(true);
echo "Time taken with $method = ".round(($end-$start)*1000.0,3)."ms\n";
}
}
$targets = [
'Huge array' => range(0, 30000),
'Small array' => range(0, 1000),
];
$methods = [
'method_1',
'method_2',
'method_3',
'method_4',
];
foreach($targets as $targetName => $target){
echo "==== Benchmark using $targetName ====\n";
benchmark($methods, $target);
echo "\n";
}
希望将来有所帮助。