In this example, I would like for a column in my TableView
to format from LocalDateTime
(which is in the source data model) to the format of "yyyy/MM/dd kk:mm" in a TableColumn
. (Note: For rendering, not editing) Well, I will need to work with Local/ZonedDateTime types in a few data models when displaying in table views. What is the best way to do this? (I did not notice an example of this type of capability in the Oracle Table View tutorial).
Edit-add: Or, maybe keeping the values in the data models as String
(formatted), and converting to LocalDateTime
for when used in processing records would be best?
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); }
}
}