0
votes

I have a TableView that is sourced from an attribute in a legacy Java Bean of type java.util.Date. I wish to customize the formatting of the date String to HH:mm:ss

I'm looking for is a native JavaFX utility to create an ObservableValue wrapper taking a java.util.DateFormat or javafx.util.StringConverter

I've found Bindings.format() class which could be used to wrap the ObservableValue, however this only allows printf format patterns like %04d etc, not any custom date specific formatting.

The best I've come up with is to use Bindings.bindBidirectional(property, property, format) with a dummy StringProperty which the CellFactory returns. Can this be simplified? Could this cause a memory leak?

public class OldBeanTableView extends Application {
    public class OldBean {
        private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
        public static final String PROPERTY_NAME_FOO = "foo";
        private Date foo = new Date();

        public Date getFoo() {
            return foo;
        }

        public void setFoo(Date foo) {
            Date oldValue = this.foo;
            this.foo = foo;
            pcs.firePropertyChange(PROPERTY_NAME_FOO, oldValue, foo);
        }

        public void addPropertyChangeListener(PropertyChangeListener listener) {
            pcs.addPropertyChangeListener(listener);
        }

        public void removePropertyChangeListener(PropertyChangeListener listener) {
            pcs.removePropertyChangeListener(listener);
        }
    }

    private class LegacyValueFactory<T, F> implements Callback<CellDataFeatures<T, String>, ObservableValue<String>> {

        private String propertyName;
        private Format format;

        public LegacyValueFactory(String propertyName, Format format) {
            this.propertyName = propertyName;
            this.format = format;
        }

        @Override
        public ObservableValue<String> call(CellDataFeatures<T, String> param) {
            try {
                Property<String> formattedString = new SimpleStringProperty();
                Property<F> original = JavaBeanObjectPropertyBuilder.create().name(propertyName).bean(param.getValue()).build();
                Bindings.bindBidirectional(formattedString, original, format);
                return formattedString;

            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        ObservableList<OldBean> beans = FXCollections.observableArrayList();
        beans.add(new OldBean());

        TableView<OldBean> tableView = new TableView<>();
        TableColumn<OldBean, String> column = new TableColumn<OldBeanTableView.OldBean, String>();
        tableView.getColumns().add(column);

        column.setCellValueFactory(new LegacyValueFactory<OldBean, String>("foo", new SimpleDateFormat("HH:mm:ss")));

        tableView.setItems(beans);
        primaryStage.setScene(new Scene(tableView));
        primaryStage.show();
        Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
                () -> beans.get(0).setFoo(new Date(beans.get(0).getFoo().getTime() + 1000)), 0, 1, TimeUnit.SECONDS);
    }

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

}
1
In this situation I would typically use a TableColumn<OldBean, Date> and a cellValueFactory that just returned the JavaBeanObjectProperty<Date> (i.e. use the Date as the data type for the column). Then also define a cellFactory to perform the formatting. Would that work, or do you really need the data type in the column to be String for some reason? - James_D
I only typed it as String because Bindings.bindBindirectory(a, b, c) forced me down that route. I thought cellFactory customization was for layout concerns, but you're right, I could use it for the string formatting... If you want to write it up as an answer I'll accept. - Adam

1 Answers

0
votes

Based on James_D suggestion this can be done using a CellFactory instead of a CellValueFactory...

public class FormattedTableCell<S, T> implements Callback<TableColumn<S, T>, TableCell<S, T>> {
    private Format format;
    public FormattedTableCell(Format format) {
        super();
        this.format = format;
    }
    @Override
    public TableCell<S, T> call(TableColumn<S, T> param) {
        return new TableCell<S, T>() {
            @Override
            protected void updateItem(T item, boolean empty) {
                super.updateItem(item, empty);
                if (item == null || empty) {
                    setText(null);
                } else {
                    setText(format.format(item));
                }
            }
        };
    }
}

Usage

column.setCellFactory(new FormattedTableCell<OldBean, Date>(new SimpleDateFormat("HH:mm:ss")));