简体   繁体   English

Javafx:从TableCell获取属性

[英]Javafx: get Property from TableCell

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: 我想从TableCell的模型中获取一个属性,因此我可以根据该属性来操作该单元格,例如下面的示例:

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> 该模型只是我所拥有的模型,我有一些模型具有两个或多个CustomProperty<Integer>

Then I have a few tables that has this Model or a similar class as model of the TableView . 然后,我有一些具有此Model或与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. 我有一个带有重写updateItem的自定义TableCell ,我想根据CustomProperty具有的属性设置单元格的文本,例如initialValue,oldValue等。例如,如果initialValue为0,则将文本设置为空而不是0默认情况下,该单元格。 I have a partial solution for it: creating an interface HasCustomProperty then the Model would implement it, but then there are a few problems: 我有一个部分解决方案:创建一个interface HasCustomProperty然后模型将其实现,但是接下来有一些问题:

  • I need to add two CustomProperties or a list of them to the interface 我需要在界面中添加两个CustomProperty或它们的列表
  • I need to ask somehow in the cell: Are you the appleCount? 我需要在单元格中询问: 您是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. 确保在一个单元格中只有一个属性,即苹果或桃子,因此从理论上讲,如果我知道它们都是CustomIntegerProperties,那么我就不必在该单元格中关心,所以我知道它们具有initialValueoldValue因此可以设置单元格的文本取决于它。

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? 我只能获取该项目,它是Integer的一种,因此我没有它的属性,或者有没有办法获取属性本身?

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. 一种解决方案是在每个列的cellFactory中重写updateItem,例如,我知道这是appleColumn,因此从appleCountProperty获取信息,依此类推,但是如果我必须这样做,则会导致很多重复的代码。 5-6个地方。 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() . 所以我以为我做了一个自定义的TableCell并在那里管理文本,所以我只为cellFactory()该单元格的每一列设置了。

Do you have any Idea how can I do it simple without duplicate code? 您有什么主意,如果没有重复的代码,我该如何简化呢?

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. 根据我们的讨论-我认为您面临的问题是确定用户集0和IntegerProperty的初始化0之间的差异。

Rather than use IntegerProperty which uses int and can't be null - you should use the following in the model: 而不是使用IntegerProperty,后者使用int且不能为null-您应该在模型中使用以下内容:

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. 这应该给您您所需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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