Steven Fowler
Steven Fowler

Reputation: 23

JavaFx: Formatting Table Column Data

This is my first time working on JavaFx and I'm following this tutorial just as a template: http://code.makery.ch/library/javafx-8-tutorial/part3/.

For my application, I'm working with 2 columns on the left side, telephone number and the call start date/time. I'm wanting to change the formatting of the data in the table as it's currently coming through as yyyy-MM-ddThh:mm.

I can't seem to figure out where to place the formatting piece at. I have a date formatter function that you can find at the link above, but it's returning a string and giving me errors. Thanks for any help you can give. Here are some code snippets of what I'm working with.

Controller:

    @FXML
private void initialize() {
    // Initialize the person table with the two columns.
    billingNumberColumn.setCellValueFactory(cellData -> cellData.getValue().billingNumberProperty());
    callStartColumn.setCellValueFactory(cellData -> cellData.getValue().callStartProperty());
}

Model:

public LocalDateTime getCallStart() {
    return callStart.get();
}

public void setCallStart(LocalDateTime callStart) {
    this.callStart.set(callStart);
}

public ObjectProperty<LocalDateTime> callStartProperty() {
    return callStart;
}

Date Format:

public static String format(ObjectProperty<LocalDateTime> callStart) {
    if (callStart == null) {
        return null;
    }
    return DATE_FORMATTER.format((TemporalAccessor) callStart);
}

Upvotes: 0

Views: 1844

Answers (2)

fabian
fabian

Reputation: 82461

Use a cellFactory. TextFieldTableCell provides a method to create a cell factory given a converter. As converter a LocalDateTimeStringConverter can be used:

callStartColumn.setCellValueFactory(cellData -> cellData.getValue().callStartProperty());
callStartColumn.setCellFactory(TextFieldTableCell.forTableColumn(new LocalDateTimeStringConverter(DATE_FORMATTER, DATE_FORMATTER)));

Upvotes: 3

Tomas Bisciak
Tomas Bisciak

Reputation: 2841

Specify column

TableColumn<Person, LocalDateTime> column = new TableColumn<>("Birth");

Code for this is quite complicated and not really good lookin.

Make sure yo utake care of empty case or / null handled when no data is in the cell

  column.setCellFactory(
                new Callback<TableColumn<Person, LocalDateTime>, TableCell<Person, LocalDateTime>>() {

                    @Override
                    public TableCell<Person, LocalDateTime> call(TableColumn<Person, LocalDateTime> param
                    ) {
                        return new TableCell<Person, LocalDateTime>() {

                            @Override
                            protected void updateItem(LocalDateTime item, boolean empty) {
                                super.updateItem(item, empty); 
                                if (item == null || empty) {
                                    setText(null);
                                    setStyle("");
                                } else {
                                    //FORMAT HERE AND CALL setText() with formatted date string



                                }
                            }

                        };
                    }
                }
        );

Upvotes: 0

Related Questions