Good day
I want to add a datepicker to the table in the cell so that the user can select a date.
I know that it is necessary to inherit an element and create my own component, but nothing came of it.
I would be happy to describe in detail the method of creating such an element.
Thanks in advance

    3 answers 3

    Something like this will look like a controller.

    @FXML private TableView<Pojo> table; @FXML private TableColumn<Pojo, String> c1; @FXML private TableColumn<Pojo, LocalDate> c2; @FXML void initialize() { c1.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().name)); c2.setCellValueFactory(param -> new SimpleObjectProperty(new DatePicker(param.getValue().localDate))); Pojo pojo = new Pojo(); pojo.name = "Name"; pojo.localDate = LocalDate.now(); table.getItems().add(pojo); } private static class Pojo { String name; LocalDate localDate; } 
    • Thanks for your reply. - Trapping

    Need CellFactory, which when editing the cell will show the datepicker. My example is a bit redundant, since My field is LocalDateTime, c LocalDate is a little easier.

      dateColumn = new TableColumn<>(); dateColumn.setEditable(true); dateColumn.setCellValueFactory(new PropertyValueFactory<>("date")); dateColumn.setCellFactory(new Callback<TableColumn<T, LocalDateTime>, TableCell<T, LocalDateTime>>() { DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd.MM.yyyy"); @Override public TableCell<T, LocalDateTime> call(TableColumn<T, LocalDateTime> param) { return new TableCell<T, LocalDateTime>() { private DatePicker datePicker; @Override protected void updateItem(LocalDateTime item, boolean empty) { if (item == getItem()) return; super.updateItem(item, empty); if (item == null) { super.setText(null); super.setGraphic(null); } else { super.setText(item.format(dtf)); super.setGraphic(null); } } @Override public void startEdit() { if (!isEditable() || !getTableView().isEditable() || !getTableColumn().isEditable()) { return; } if(datePicker == null){ datePicker = createPicker(); } datePicker.setValue(getItem().toLocalDate()); super.startEdit(); setText(null); setGraphic(datePicker); } @Override public void cancelEdit() { super.cancelEdit(); setText(getItem().format(dtf)); setGraphic(null); } private DatePicker createPicker() { DatePicker picker = new DatePicker(); picker.setOnAction(event -> { this.commitEdit(picker.getValue().atStartOfDay()); event.consume(); }); picker.setOnKeyReleased(event -> { if (event.getCode() == KeyCode.ESCAPE) { this.cancelEdit(); event.consume(); } }); return picker; } }; } }); 

      In the eighth java there is a built-in component DatePicker .