简体   繁体   中英

How to change view of object in table cell? - JavaFx

I have a column of LocalDateTime in my TableView :

column_AddDate = new TableColumn<MyTableItem, LocalDateTime>(TITLE_ADD_DATE)

But result - 2016-02-05T12:26:20.506 - is not so pretty as for me. I want to store and sort table data using rules of LocalDateTime class, but show it with custom format. For example:

formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
formattedDateTime = dateTime.format(formatter); // "2016-02-05 12:26"

First, that I've tryed, is to write custom class DateTimeForTable extends LocalDateTime and override toString() method, but LocalDateTime is final.

So, I think, solution is in using setCellValueFactory() method and similar, but I'm not skilled in this.

Please tell me, how to realize this feature.

To change the way the data is presented, rather than the data itself, you should use a cellFactory . (I assume you already have a cellValueFactory installed, else you would not see any data at all. Note the cellFactory is in addition to the cellValueFactory , it does not replace it.)

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
column_AddDate.setCellFactory(tc -> new TableCell<MyTableItem, LocalDateTime>() {
    @Override
    protected void updateItem(LocalDateTime item, boolean empty) {
        super.updateItem(item, empty);
        setText(empty ? null : formatter.format(item));
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM