在JavaFX中求和特定的TableView列/行 [英] Suming a specific TableView column/row in JavaFX

查看:104
本文介绍了在JavaFX中求和特定的TableView列/行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在java中完成了此操作,在这里我将price列/行的值相加.但是我想知道如何在JavaFX中执行此操作.

I have done this in java where I sum the values of the price column/row. But I am wondering how to do this in JavaFX.

我想对第1列中的所有内容求和并将其显示在inputField中,我曾在Java中使用它来完成此操作,但是如何在JavaFX中做到这一点呢?

I want to sum everything in column 1 and display it in a inputField, I used this to do it in java but how do you do this in JavaFX?

tableview.getValueAt(i, 1).toString();

这就是我想要做的:

int sum = 0;

for (int i = 0; i < tableview.getItems().size(); i++) {
    sum = sum + Integer.parseInt(tableview.getValueAt(i, 1).toString());
}

sumTextField.setText(String.valueOf(sum));

推荐答案

如果您确实有一个TableView<Integer>,这似乎就是您在评论中所说的,那么您就可以这样做

If you really have a TableView<Integer>, which seems to be what you are saying in the comments, you can just do

TableView<Integer> table = ... ;

int total = 0 ;
for (Integer value : table.getItems()) {
    total = total + value;
}

或使用Java 8方法:

or, using a Java 8 approach:

int total = table.getItems().stream().summingInt(Integer::intValue);

如果您为表设置了更为标准的模型,则需要使用其实际模型类,那么您将需要遍历项目列表并在每个项目上调用适当的get方法,然后将结果添加到总计中.例如.像

If you have a more standard set up with an actual model class for your table, then you would need to iterate through the items list and call the appropriate get method on each item, then add the result to the total. E.g. something like

TableView<Item> table = ...;

int total = 0 ;
for (Item item : table.getItems()) {
    total = total + item.getPrice();
}

或者再次使用Java 8样式

or, again in Java 8 style

int total = table.getItems().stream().summingInt(Item::getPrice);

这两个方法都假设您拥有一个带有getPrice()方法的Item类,并且所讨论的列正在显示每个项目的price属性.

Both of these assume you have an Item class with a getPrice() method, and the column in question is displaying the price property of each item.

这篇关于在JavaFX中求和特定的TableView列/行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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