如何使用对象将数据添加到 JavaFX 中的空表视图中?
How do I use object to add data into an empty tableview in JavaFX?
我想做的是创建一个 add 来计算给定贷款金额、利率和期限的每月抵押付款。但是我在向表视图中添加数据时遇到了很多问题。我创建了一个名为 "Payment" 的 class,我所做的是在控制器中 class 我创建了一个名为 "payments" 的支付对象数组。每个 Payment 对象代表表视图中的一行。然后我使用
创建了一个 observedArrayList
ObservableList items=FX.Collections.observatbleArrayList(付款)
并使用 tableview.setItems(items).
将 ObservableList 添加到表视图中
但是每行中只显示每个付款对象的 2 个值。即,每行仅显示 "month" 和 "remaining value" 的值。但剩余价值显示在 "payment" 的列下方,因此它出现在错误的列下方。请在下面查看我的控制器中的代码以及我的付款 class:
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
public class MortgagecalculatorController implements Initializable {
@FXML
private TextField loanAmount;
@FXML
private ComboBox<Double> interest;
@FXML
private ComboBox<Integer> mTerm;
@FXML
private Button calculateButton;
@FXML
private TextField totalPayment;
@FXML
private TableView<Payment> tView;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
for (double i = 3.25; i <= 5; i = i + 0.01) {
double temp = ((double) Math.round(i * 100)) / 100;
interest.getItems().add(temp);
}
mTerm.getItems().addAll(5, 10, 15, 30);
TableColumn monthColumn = new TableColumn("Month Number");
monthColumn.setPrefWidth(100);
monthColumn.setCellValueFactory(new PropertyValueFactory<>("month"));
TableColumn paymentColumn = new TableColumn("Payment");
paymentColumn.setPrefWidth(140);
paymentColumn.setCellValueFactory(new PropertyValueFactory<>("monthlyPayment"));
TableColumn principleColumn = new TableColumn("Principle Paid");
principleColumn.setPrefWidth(140);
paymentColumn.setCellValueFactory(new PropertyValueFactory<>("principle"));
TableColumn interestColumn = new TableColumn("Interest Paid");
interestColumn.setPrefWidth(140);
paymentColumn.setCellValueFactory(new PropertyValueFactory<>("interest"));
TableColumn totalInterestColumn = new TableColumn("Total Interest Paid");
totalInterestColumn.setPrefWidth(140);
paymentColumn.setCellValueFactory(new PropertyValueFactory<>("total"));
TableColumn remainingColumn = new TableColumn("Remaining Value");
remainingColumn.setPrefWidth(140);
paymentColumn.setCellValueFactory(new PropertyValueFactory<>("remaining"));
tView.getColumns().addAll(monthColumn, paymentColumn, principleColumn, interestColumn, totalInterestColumn, remainingColumn);
interest.setValue(3.25);
mTerm.setValue(5);
}
@FXML
private void handleCalculateButton(ActionEvent event) {
double loan;
if (loanAmount.getText().isEmpty()) {
System.out.println("Please enter loan amount");
return;
}
if (isNumeric(loanAmount.getText())) {
loan = Double.parseDouble(loanAmount.getText());
} else {
System.out.println("loan amount entered is non-numeric");
return;
}
int months = mTerm.getValue() * 12;
double monthlyInterest = (interest.getValue() / 100) / 12;
double monthlyPayment = loan * monthlyInterest / (1 - (Math.pow(1 / (1 + monthlyInterest), months)));
Payment[] payments = new Payment[months];
double rValue = loan;
double totalInterest = 0;
System.out.println("monthly payment: "+monthlyPayment);
for (int i = 0; i < payments.length; i++) {
double interestPaid = rValue * monthlyInterest;
double principle = monthlyPayment - interestPaid;
totalInterest = totalInterest + interestPaid;
rValue = rValue - principle;
payments[i] = new Payment(String.valueOf(i + 1), monthlyPayment, principle, interestPaid, totalInterest, rValue);
}
ObservableList<Payment> items = FXCollections.observableArrayList(payments);
tView.setItems(items);
}
public static boolean isNumeric(String str) {
try {
Double.parseDouble(str);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}
public class Payment {
private String month;
private double monthlyPayment;
private double principle;
private double interest;
private double total;
private double remaining;
public Payment(String mon, double pay,double principle, double interest,double totalInterest, double rValue ){
this.month=mon;
this.monthlyPayment=pay;
this.principle=principle;
this.interest=interest;
this.total=totalInterest;
this.remaining=rValue;
}
public String getMonth(){
return month;
}
public double getMonthlyPayment(){
return monthlyPayment;
}
public double getPrinciple(){
return principle;
}
public double getInterest(){
return interest;
}
public double getTotal(){
return total;
}
public double getRemaining(){
return remaining;
}
}
您通过(很可能是复制粘贴)此行不断覆盖 paymentColumn 的 CellValueFactory:
paymentColumn.setCellValueFactory(new PropertyValueFactory<>("monthlyPayment"));
您正在更改值,而不是 TableColumn。
对于从 principleColumn 开始的每个 TableColumn,您需要设置正确的 CellValueFactory。例如,对于 principleColumn,您需要这样做:
TableColumn principleColumn = new TableColumn("Principle Paid");
principleColumn.setPrefWidth(140);
principleColumn.setCellValueFactory(new PropertyValueFactory<>("principle"));
注意最后一行,在这种情况下你需要使用principleColumn.setCellValueFactory(...)。
我想做的是创建一个 add 来计算给定贷款金额、利率和期限的每月抵押付款。但是我在向表视图中添加数据时遇到了很多问题。我创建了一个名为 "Payment" 的 class,我所做的是在控制器中 class 我创建了一个名为 "payments" 的支付对象数组。每个 Payment 对象代表表视图中的一行。然后我使用
创建了一个 observedArrayListObservableList items=FX.Collections.observatbleArrayList(付款)
并使用 tableview.setItems(items).
将 ObservableList 添加到表视图中但是每行中只显示每个付款对象的 2 个值。即,每行仅显示 "month" 和 "remaining value" 的值。但剩余价值显示在 "payment" 的列下方,因此它出现在错误的列下方。请在下面查看我的控制器中的代码以及我的付款 class:
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
public class MortgagecalculatorController implements Initializable {
@FXML
private TextField loanAmount;
@FXML
private ComboBox<Double> interest;
@FXML
private ComboBox<Integer> mTerm;
@FXML
private Button calculateButton;
@FXML
private TextField totalPayment;
@FXML
private TableView<Payment> tView;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
for (double i = 3.25; i <= 5; i = i + 0.01) {
double temp = ((double) Math.round(i * 100)) / 100;
interest.getItems().add(temp);
}
mTerm.getItems().addAll(5, 10, 15, 30);
TableColumn monthColumn = new TableColumn("Month Number");
monthColumn.setPrefWidth(100);
monthColumn.setCellValueFactory(new PropertyValueFactory<>("month"));
TableColumn paymentColumn = new TableColumn("Payment");
paymentColumn.setPrefWidth(140);
paymentColumn.setCellValueFactory(new PropertyValueFactory<>("monthlyPayment"));
TableColumn principleColumn = new TableColumn("Principle Paid");
principleColumn.setPrefWidth(140);
paymentColumn.setCellValueFactory(new PropertyValueFactory<>("principle"));
TableColumn interestColumn = new TableColumn("Interest Paid");
interestColumn.setPrefWidth(140);
paymentColumn.setCellValueFactory(new PropertyValueFactory<>("interest"));
TableColumn totalInterestColumn = new TableColumn("Total Interest Paid");
totalInterestColumn.setPrefWidth(140);
paymentColumn.setCellValueFactory(new PropertyValueFactory<>("total"));
TableColumn remainingColumn = new TableColumn("Remaining Value");
remainingColumn.setPrefWidth(140);
paymentColumn.setCellValueFactory(new PropertyValueFactory<>("remaining"));
tView.getColumns().addAll(monthColumn, paymentColumn, principleColumn, interestColumn, totalInterestColumn, remainingColumn);
interest.setValue(3.25);
mTerm.setValue(5);
}
@FXML
private void handleCalculateButton(ActionEvent event) {
double loan;
if (loanAmount.getText().isEmpty()) {
System.out.println("Please enter loan amount");
return;
}
if (isNumeric(loanAmount.getText())) {
loan = Double.parseDouble(loanAmount.getText());
} else {
System.out.println("loan amount entered is non-numeric");
return;
}
int months = mTerm.getValue() * 12;
double monthlyInterest = (interest.getValue() / 100) / 12;
double monthlyPayment = loan * monthlyInterest / (1 - (Math.pow(1 / (1 + monthlyInterest), months)));
Payment[] payments = new Payment[months];
double rValue = loan;
double totalInterest = 0;
System.out.println("monthly payment: "+monthlyPayment);
for (int i = 0; i < payments.length; i++) {
double interestPaid = rValue * monthlyInterest;
double principle = monthlyPayment - interestPaid;
totalInterest = totalInterest + interestPaid;
rValue = rValue - principle;
payments[i] = new Payment(String.valueOf(i + 1), monthlyPayment, principle, interestPaid, totalInterest, rValue);
}
ObservableList<Payment> items = FXCollections.observableArrayList(payments);
tView.setItems(items);
}
public static boolean isNumeric(String str) {
try {
Double.parseDouble(str);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}
public class Payment {
private String month;
private double monthlyPayment;
private double principle;
private double interest;
private double total;
private double remaining;
public Payment(String mon, double pay,double principle, double interest,double totalInterest, double rValue ){
this.month=mon;
this.monthlyPayment=pay;
this.principle=principle;
this.interest=interest;
this.total=totalInterest;
this.remaining=rValue;
}
public String getMonth(){
return month;
}
public double getMonthlyPayment(){
return monthlyPayment;
}
public double getPrinciple(){
return principle;
}
public double getInterest(){
return interest;
}
public double getTotal(){
return total;
}
public double getRemaining(){
return remaining;
}
}
您通过(很可能是复制粘贴)此行不断覆盖 paymentColumn 的 CellValueFactory:
paymentColumn.setCellValueFactory(new PropertyValueFactory<>("monthlyPayment"));
您正在更改值,而不是 TableColumn。
对于从 principleColumn 开始的每个 TableColumn,您需要设置正确的 CellValueFactory。例如,对于 principleColumn,您需要这样做:
TableColumn principleColumn = new TableColumn("Principle Paid");
principleColumn.setPrefWidth(140);
principleColumn.setCellValueFactory(new PropertyValueFactory<>("principle"));
注意最后一行,在这种情况下你需要使用principleColumn.setCellValueFactory(...)。