我有一个带桌子的应用程序。在这个表中,我计算了一些字段,这些字段在数据类中不存在,并且取决于其他行值,例如当前行号或前一行的总和。如何在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
答案 0 :(得分:3)
非常有趣的问题我觉得这个解决方案对你有用
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();
}
}
}