在TableView列中格式化ObjectProperty <LocalDateTime>

Mar*_*ers 4 java datetime javafx tableview

在这个例子中,我希望我的一个列TableViewLocalDateTime(在源数据模型中)格式化为格式为"yyyy/MM/dd kk:mm" TableColumn.(注意:对于渲染,而不是编辑)好吧,在表视图中显示时,我需要在一些数据模型中使用Local/ZonedDateTime类型.做这个的最好方式是什么?(我没有注意到Oracle Table View教程中此类功能的示例).

编辑添加:或者,可能将数据模型中的值保持为String(格式化),并LocalDateTime在处理记录时使用转换为最佳?

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); }

    }
}
Run Code Online (Sandbox Code Playgroud)

DVa*_*rga 8

您可以使用TableColumnas 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)));
    }
});
Run Code Online (Sandbox Code Playgroud)

或者,您可以将a DateTimeFormatter转换LocalDateTime为a String,但在这种情况下,表排序将不起作用(将使用字符串排序).谢谢@JFValdes指出这一点.

在这种情况下,你可以使用setCellValueFactory的方法TableColumn来显示它作为一个StringTableView.

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)));
Run Code Online (Sandbox Code Playgroud)