java - 如何计算 JavaFX TableView 中依赖于其他行的单元格的值?

标签 java javafx tableview cell

我有一个带有表格的应用程序。在此表中,我计算了一些字段,这些字段在数据类中不存在并且取决于其他行值,例如当前行号,或先前行的总和。如何在 TableView 中计算这个值?

问题是我没有有关单元格值工厂中当前行号的信息。

示例:

public class Transaction {  
     ...  
     public String getName();  
     public BigInteger getAmount();  
     ...  
} // There is no getter for "Balance"

结果表应该是这样的:

Name           |   Amount  |    ... | Balance
transaction1   |    300    |    ... |   300
transaction2   |    200    |    ... |   500
transaction3   |    500    |    ... |   1000

此外,按“金额”排序后,应重新计算“余额”:

Name           |   Amount  |    ... | Balance
transaction3   |    500    |    ... |   500
transaction1   |    300    |    ... |   800
transaction2   |    200    |    ... |   1000

最佳答案

非常有趣的问题,我认为这个解决方案对您有用

import javafx.application.Application;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Callback;

/**
 *
 * @author reegan
 */
public class CalculateTableCellStackover extends Application {

    @Override
    public void start(Stage primaryStage) {

        Transcation t1 = new Transcation("Transcation1", 100);
        Transcation t2 = new Transcation("Transcation2", 500);
        Transcation t3 = new Transcation("Transcation3", 300);
        Transcation t4 = new Transcation("Transcation4", 1000);
        Transcation t5 = new Transcation("Transcation5", 200);
        ObservableList<Transcation> list = FXCollections.observableArrayList();
        list.addAll(t1, t2, t3, t4, t5);



        TableView<Transcation> tableView = new TableView<Transcation>();
        TableColumn name = new TableColumn("Name");
        name.setCellValueFactory(new PropertyValueFactory<Transcation, String>("name"));
        TableColumn amount = new TableColumn("Amount");
        amount.setCellValueFactory(new PropertyValueFactory<Transcation, Integer>("amount"));
        TableColumn balance = new TableColumn("Balance");
        balance.setMinWidth(50);
        balance.setCellValueFactory(new Callback<TableColumn.CellDataFeatures, ObservableValue>() {
            @Override
            public ObservableValue call(TableColumn.CellDataFeatures p) {
                return new ReadOnlyObjectWrapper(p.getValue());
            }
        });
//        
        balance.setCellFactory(new Callback<TableColumn, TableCell>() {
            @Override
            public TableCell call(TableColumn p) {
                return new TableCell() {
                    @Override
                    protected void updateItem(Object item, boolean empty) {
                        super.updateItem(item, empty);
                        if (this.getTableRow() != null && item != null) {
//                            System.out.println(this.getTableRow().getItem().toString());
//                            setText((this.getTableRow().getIndex() + 1) + "");
//                              System.out.println(this.getTableRow().getIndex());
//                            System.out.println(getTableView().getItems().get(getTableRow().getIndex() -1));
//                            System.out.println();
                            int current = this.getTableRow().getIndex();
                            int pre = this.getTableRow().getIndex() - 1;
                            if(current == 0) {
                                pre = current;
                            }
                            System.out.println("");
//                            setText(String.valueOf(Integer.parseInt(getTableRow().getItem().toString()) + Integer.parseInt(getTableView().getItems().get(pre).toString())));
                            Integer totalValue = new Integer(0);
                            for(int i = 0; i <= current;i++) {  
                               totalValue = totalValue + (Integer.parseInt(getTableView().getItems().get(i).toString()));
                            }

                            setText(String.valueOf(totalValue));


//                            setText(this.getTableRow().getItem().toString());
                        } else {
                            setText("");
                        }
                    }
                };
            }
        });
        tableView.getColumns().addAll(name, amount, balance);
        tableView.setItems(list);
        StackPane root = new StackPane();
        root.getChildren().add(tableView);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    public class Transcation {

        String name;
        Integer amount;

        public Transcation(String name, int amount) {
            this.name = name;
            this.amount = amount;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAmount() {
            return amount;
        }

        public void setAmount(int amount) {
            this.amount = amount;
        }

        @Override
        public String toString() {
            return amount.toString();
        }
    }
}

关于java - 如何计算 JavaFX TableView 中依赖于其他行的单元格的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20881556/

相关文章:

java - 使用 Jackson JSON 反序列化和嵌套类?

java - 使用分页获取组成员 - unboundid

java - 每次调用方法时尝试使新对象显示在不同的位置(JavaFX)

ios - 动态静态 TableView

javafx 事件不工作

java - Spring自定义错误信息

javafx - 应该将什么传递给 FileChooser?

JavaFX TextArea appendText CPU 使用率高

ios - 如何在展开 subview 时在 TableView 单元格中添加 subview 并对 subview 的元素执行操作?

tableview - 从模型自动更新 TableView 中的行