1
votes

My question is related to TableView from JavaFx. JavaFx recommends using Properties for storing values which are going to be shown in the table ( SimpleSttringProperty, etc. ) like in the example below.

Isn't this an memory overhead for Java ( creating another object for each cell ) ? I am going to use the TableView to show more then 10.000 rows from the database. Isn't there a way to avoid this, and simply work as Swing did earlier ? Can't TableView get only the value to represent ? For all solutions I found an ObservableValue is required for each cell, which may require a lot of memory.

TableColumn<Person,String> firstNameCol = new TableColumn<Person,String>("First Name");
firstNameCol.setCellValueFactory( element -> element.getValue().firstName );
or 
firstNameCol.setCellValueFactory( new PropertyValueFactory<Person, String>("firstName"));
public static class Person {

    public final SimpleStringProperty firstName;
    private final SimpleStringProperty lastName;
    private final SimpleStringProperty email;

    private Person(String fName, String lName, String email) {
        this.firstName = new SimpleStringProperty(fName);
        this.lastName = new SimpleStringProperty(lName);
        this.email = new SimpleStringProperty(email);
    }
1
The memory consumption of a Property is pretty minimal, isn't it? It's just a simple object wrapping the value, so should only consume a few more bytes than the value itself. Do you have an actual example where you can show this causes a problem? - James_D
@James_D There can be few bytes. This is an object that performs callback notifications. In this scenario, they sit permanently in memory. In the example I've just given below, only the objects that will be used are created (the others are cleaned by GC). Thus, for millions of items only 50-100 objects will be used (roughly) - mr mcwolf
@mrmcwolf Yes, understood. But for any table that is remotely usable, we are only talking about a difference of ~1MB, no? Even on a memory-limited device, I can't see that making any appreciable difference. Isn't it better to follow the intended pattern (so, for example, editing will work out-of-the-box, if the OP wants that, now or later) than to compromise that without a pressing need to do so? - James_D

1 Answers

0
votes

You are not required to use property in your model. Here is an example

public class Person {

    private String firstName;
    private String lastName;
    private String email;

    public Person() {
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

just create the property when you need it.

firstNameCol.setCellValueFactory(cdf -> new SimpleStringProperty(cdf.getValue().getFirstName()));