我有 javafx 表,其中包括组织的成员每月费用。在此表中,我显示了一些带有默认金额的费用,以便在任何情况下用户都可以更新金额或按原样继续。我真正需要的是,如果用户更新任何值,这些值应该立即绑定到表 observablelist。
创建表:
private void initCollectionTable(ObservableList<Member> mList) {
...
total_pay_col.setCellValueFactory(new SubscriptionValueFactory());
detail_view_col.setCellValueFactory(new DisplaySubscriptionFactory());
...
collection_tbl.setItems(mlz);
}
订阅值工厂类:
public class SubscriptionValueFactory implements Callback<TableColumn.CellDataFeatures<Member, String>, ObservableValue<String>> {
@Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<Member, String> param) {
//Here Subscriptions are equal to various member charges
Member ml = param.getValue();
List<MemberSubscriptions> mbrSubs = new ArrayList<>(ml.getMemberSubscriptions());
double sum =mbrSubs.stream().mapToDouble(a -> a.getAmount()).sum();
return new SimpleObjectProperty<>(TextFormatHandler.CURRENCY_DECIMAL_FORMAT.format(sum));
}
}
DisplaySubscriptionFactory 类
public class DisplaySubscriptionFactory implements Callback<TableColumn.CellDataFeatures<Member, Button>, ObservableValue<Button>> {
@Override
public ObservableValue<Button> call(TableColumn.CellDataFeatures<Member, Button> param) {
Member ml = param.getValue();
List<MemberSubscriptions> mbrSubs = new ArrayList<>(ml.getMemberSubscriptions());
double sum =mbrSubs.stream().mapToDouble(a -> a.getAmount()).sum();
boolean flag = // SOME BOOLEAN CHECK HERE-----
param.getValue().setTotalSubscription(sum);
Button button = new Button("View Info");
button.setOnAction((evt) -> {
Alert alert_details = new Alert(Alert.AlertType.INFORMATION);
alert_details.setTitle("Subscription Information");
alert_details.setHeaderText("Member Subscription information for each installment");
alert_details.getDialogPane().setContent(createContentGrid(mbrSubs, sum, flag));
alert_details.show();
});
return new SimpleObjectProperty<>(button);
}
此方法创建一个网格以显示文本字段中的成员费用。因此,文本字段内的任何值更改都应更新其自己的表observablelist属性。
private Node createContentGrid(List<MemberSubscriptions> mbrSubs, double sum, boolean flag) {
GridPane grid = new GridPane();
grid.setHgap(20);
grid.setVgap(10);
grid.setPadding(new Insets(20, 20, 10, 10));
Label totLabel = new Label(TextFormatHandler.CURRENCY_DECIMAL_FORMAT.format(sum));
totLabel.setFont(Font.font("System Bold", 21.0));
Label col_h_1 = new Label("Subscription");
col_h_1.setFont(Font.font("System Bold", 17.0));
Label col_h_2 = new Label("Amount");
col_h_2.setFont(Font.font("System Bold", 17.0));
grid.add(col_h_1, 0, 0);
grid.add(col_h_2, 1, 0);
Label[] labels = new Label[mbrSubs.size()];
TextField[] fields = new TextField[mbrSubs.size()];
for (int i = 0; i < mbrSubs.size(); i++) {
MemberSubscriptions get = mbrSubs.get(i);
labels[i] = new Label("label");
fields[i] = new TextField();
fields[i].setTextFormatter(TextFormatHandler.currencyFormatter());
labels[i].setText(get.getMemberSubscription().getFeeName());
fields[i].setText(TextFormatHandler.CURRENCY_DECIMAL_FORMAT.format(get.getAmount()));
grid.add(labels[i], 0, i + 1);
grid.add(fields[i], 1, i + 1);
}
grid.add(totLabel, 1, mbrSubs.size() + 1);
return grid;
}
会员等级:
public class Member {
private Integer id;
private String memberId;
...
private Set<MemberSubscriptions> memberSubscriptions = new HashSet<>();
...
会员订阅类
public class MemberSubscriptions {
private Integer id;
private Member member;
private Double amount;
...