在TableView中使用PropertyValueFactory中的值-JavaFx [英] Work with Value from PropertyValueFactory in TableView - JavaFx

查看:232
本文介绍了在TableView中使用PropertyValueFactory中的值-JavaFx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的JavaFx项目中,我有一个带有一些视图的场景.对于窗口的页脚,我有一个从TableView扩展的类:

In my JavaFx project I have a scene with a few views. For the footer of the window I have a class extending from TableView:

public class FooterView extends TableView<Area> implements ViewTemplate {...}

这会显示一个表格,其中包含来自.csv文件的一些数据.

This displays a Table with some data from a .csv-file.

在将特定presentationmodel属性的值分配给单元格时,我这样做是这样的:

When it comes to assigning the value of the specific presentationmodel property to the cells I do it like that:

TableColumn<Area,Double> powerColumn = new TableColumn<>("Power Overview");
powerColumn.setCellValueFactory(new PropertyValueFactory<>("powerPerArea"));

this.setItems(filePM.getAreas()); //filePm is my filehandler
this.getColumns().addAll(powerColumn, other_columns);

getAreas()看起来像这样:

public List<Area> readCantonsFromFile() {
        try (Stream<String> stream = getStreamOfLines(AREA_FILE_NAME)) {
            return stream.skip(1)
                    .map(l -> new Area(l.split(DELIMITER, 12)))
                    .collect(Collectors.toList());
        }
}

Area的构造函数中,我设置属性.属性之一是提到的powerPerArea

In the constructor of Area i set the properties. One of the properties is the mentioned powerPerArea

private final DoubleProperty powerPerArea = new SimpleDoubleProperty();
...
public void setPowerPerCanton(double powerPerCanton) {
    this.powerPerCanton.set(powerPerCanton);
}

我的问题:是否可以在显示值之前在FooterView中更改该值?我尝试过这样的事情:

My question: Is there a way to change the value in the FooterView before the value is displayed? I tried something like this:

powerColumn.setCellValueFactory(Math.round(new PropertyValueFactory<>("powerPerArea")));

但是似乎我混合使用了DoubleProperty,Double和ObservableDouble.我什至可以在这里修改值吗?

But it seems that I mixed up DoubleProperty, Double and ObservableDouble. Can I even modify the value in here?

问题:我无法在setter中舍入该值,因为我通过该函数在循环中添加了一个double值:

The problem: I can not round the value in the setter because I add a double value in a loop through this function:

public void addPowerPerArea(double power){
    double sum = getPowerPerCanton() + power;
    setPowerPerCanton(sum);
}

在这里四舍五入的值会给我一个错误的结果. (四舍五入不够精确).最后,当所有金额相加后,我需要这样做

And rounding the value in here would give me a wrong result. (rounding not precise enough). I need to do it in the end when all sums are added

推荐答案

您应该使用cellValueFactory来确定哪些数据显示在单元格中:在这种情况下,您的PropertyValueFactory是从powerPerAreaProperty().get()返回的实际double值,正是您想要的.

You should use the cellValueFactory to determine which data are displayed in the cells: in this case the data returned by your PropertyValueFactory is the actual double value returned from powerPerAreaProperty().get(), which is exactly what you want.

如果要控制如何显示数据,则应使用cellFactory.因此,要以特定格式显示数据(包括限制小数位数),可以执行以下操作:

If you want to control how the data are displayed, you should use a cellFactory. So to display the data in a particular format, including limiting the number of decimal places, you can do:

powerColumn.setCellFactory(tc -> new TableCell<Area, Double>() {
    @Override
    protected void updateItem(Double power, boolean empty) {
        super.updateItem(power, empty);
        if (empty) {
            setText(null);
        } else {
            setText(String.format("%.0f", power.doubleValue()));
        }
    }
});

这里的要点是,您不应根据想要的显示方式修改数据;同时使用cellValueFactorycellFactory的目的是将数据的显示与实际数据本身分开.

The point here is that you should not modify the data based on how you want to display it; the purpose of having both cellValueFactory and cellFactory is to separate the display of the data from the actual data itself.

这篇关于在TableView中使用PropertyValueFactory中的值-JavaFx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆