Formatting ObjectProperty <LocalDateTime> in a TableView column

In this example, I would like the column in mine to TableViewformat from LocalDateTime(which is in the original data model) to the format "yyyy / MM / dd kk: mm" in TableColumn. (Note: for rendering, not for editing) Well, I will need to work with Local / ZonedDateTime types in several data models when displayed in table views. What is the best way to do this? (I did not notice an example of this type of feature in the Oracle Table View tutorial).

Edit-add: Or, maybe saving the values ​​in the data models as String(formatted) and converting LocalDateTimeto those used in the processing of records would be better?

import java.time.LocalDateTime;

import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage stage) {

        final ObservableList<Foo> data = FXCollections.observableArrayList();
        LocalDateTime ldt = LocalDateTime.now();
        data.add(new Foo(ldt));
        data.add(new Foo(ldt.plusDays(1)));
        data.add(new Foo(ldt.plusDays(2)));

        TableView<Foo> table = new TableView<Foo>();
        table.setItems(data);

        TableColumn<Foo, LocalDateTime> ldtCol = new TableColumn<Foo, LocalDateTime>("LDT");
        // --- Set cell factory value ---

        table.getColumns().addAll(ldtCol);
        Scene scene = new Scene(table);
        stage.setScene(scene);
        stage.show();
    } 

    public static void main(String[] args) {
        launch(args);
    }

    class Foo {
        private final ObjectProperty<LocalDateTime> ldt = 
                new SimpleObjectProperty<LocalDateTime>();

        Foo(LocalDateTime ldt) {
            this.ldt.set(ldt);
        }

        public ObjectProperty<LocalDateTime> ldtProperty() { return ldt; }
        public LocalDateTime getLdt() { return ldt.get(); }
        public void setLdt(LocalDateTime value) { ldt.set(value); }

    }
}
+4
1

TableColumn TableColumn<Foo, LocalDateTime>: LocalDateTime , cellFactory :

TableColumn<Foo, LocalDateTime> ldtCol = new TableColumn<Foo, LocalDateTime>("LDT");
ldtCol.setCellValueFactory(cellData -> cellData.getValue().ldtProperty());
ldtCol.setCellFactory(col -> new TableCell<Foo, LocalDateTime>() {
    @Override
    protected void updateItem(LocalDateTime item, boolean empty) {

        super.updateItem(item, empty);
        if (empty)
            setText(null);
        else
            setText(String.format(item.format(formatter)));
    }
});

DateTimeFormatter LocalDateTime String, n case ( ). @JFValdes, .

setCellValueFactory TableColumn, String TableView.

private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd hh:mm");

...    

TableColumn<Foo, String> ldtCol = new TableColumn<Foo, String>("LDT");
ldtCol.setCellValueFactory(foo -> new SimpleStringProperty(foo.getValue().getLdt().format(formatter)));
+7

All Articles