gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:31:40 25 4
gpt4 key购买 nike

我有一个带表的应用程序。在此表中,我计算了一些字段,这些字段在数据类中不存在并且取决于其他行值,例如当前行号,或前几行的总和。我如何在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com