0
votes

I want to get a property from a model in my TableCell, so I can maipulate the cell depending on that property lets se the following example:

I have a model like:

public class Model {

    private CustomProperty<Integer> appleCount;
    private CustomProperty<Integer> peachCount;

    public Model(Integer appleCount, Integer peachCount) {
        this.appleCount = new CustomIntegerProperty(appleCount);
        this.peachCount = new CustomIntegerProperty(peachCount);
    }

    public CustomProperty<Integer> appleCountProperty() {
        return appleCount;
    }

    public CustomProperty<Integer> peachCountProperty() {
        return peachCount;
    }
}

This model is just a mock of that I have, and I have a few models which have two or more CustomProperty<Integer>

Then I have a few tables that has this Model or a similar class as model of the TableView. I have a custom TableCell with overriden updateItem and there I want to set the cell's text depending on the properties that CustomProperty has for example initialValue, oldValue, etc.Fo example if the initialValue is 0 then set the text empty instead of the 0 that has the cell by default. I have a partial solution for it: creating an interface HasCustomProperty then the Model would implement it, but then there are a few problems:

  • I need to add two CustomProperties or a list of them to the interface
  • I need to ask somehow in the cell: Are you the appleCount? or Are you the peach cell?

It is sure that in one cell there is only one property, the apple or the peach, so theoretically I should not care about in the cell if I know both of them are CustomIntegerProperties so I know they have initialValue or oldValue so I can set the cell's text depending on it.

I can only get the item, which is a type of Integer, so I don't have the properties of it or is there any way to get the property itself?

A sollution could be to override in every column's cellFactory the updateItem, and there I know for instance this is the appleColumn so get the information from the appleCountProperty, and so on, but this causes a lot of duplicate code if I have to do it in 5-6 places. So I thought I make a custom TableCell and there I manage the text, then I just set for every column that cell for cellFactory().

Do you have any Idea how can I do it simple without duplicate code?

1
I wasn't quite sure exactly the question you are asking.... I provided an answer on how to give the cell the cell the entire object for query during update item. If this is not what you are looking for, Please let me know and I can hopefully point you in a different direction. - purring pigeon

1 Answers

1
votes

Based on our discussion - I think the issue you are facing is determining the difference between a user set 0 and an initialized 0 of an IntegerProperty.

Rather than use IntegerProperty which uses int and can't be null - you should use the following in the model:

private ObjectProperty<Integer> appleCountProperty = new SimpleObjectProperty<>();

Then in your table you just bind to it:

@FXML
TableColumn<Model, Integer> appleCountColumn;

//In your initialize

appleCountColumn.setCellValueFactory(data -> data.getValue().appleCountProperty ());

This should give you what you need.