It's hard to find examples that address issues specifically when using the prebuilt components in NetBeans.
I have added a Table to my GUI. I then went to the "model" property and removed the rows and one of the columns and then customized my column names.
There is a single button that affects all of this code. This is a school project, so it's rather simple. I'm just having trouble getting my JTable to be updated after I add new rows.
I've tried various things people have mentioned on forums, but just can't seem to get my Table to update. When I run my program, I don't get any errors when I click on my button, it just calculates the Monthly Payment, and does nothing with the Table. I'm hoping I'm even updating a row properly and that I'm simply having troubles with the Table displaying it.
So, here is my code with the lines bolded that are related to the table; hopefully someone can point in me in the correct direction:
[B]import javax.swing.table.DefaultTableModel;[/B]
private void btnCalculateActionPerformed(java.awt.event.ActionEvent evt) {
double principleValue = 200000.00;
double[] APR = {5.35, 5.5, 5.75};
int[] termLength = {7, 15, 30};
int i = cboLoans.getSelectedIndex();
double interestRate = APR[i] / 100 / 12;
int notePeriod = termLength[i] * 12;
double monthlyPayment = calculateMonthlyPayment(principleValue, interestRate, notePeriod);
this.txtMonthlyPayment.setText(Currency.format(monthlyPayment));
//Initialize values for Amortization Table
double loanBalance = notePeriod * monthlyPayment;
double principalBalance = principleValue;
double interestBalance = loanBalance - principalBalance;
double totalPrincipalPaid = 0;
double totalInterestPaid = 0;
[B]DefaultTableModel model = (DefaultTableModel) tblAmortization.getModel();[/B]
for (int x = 1; x < notePeriod + 1; x++) {
//The portion of the payment that goes to interest is based on the remaining principal balance.
double interestPaidThisMonth = interestRate * principalBalance;
//The rest of the payment goes to principal.
double principalPaidThisMonth = monthlyPayment - interestPaidThisMonth;
//Update the remaining balances and total paid values
totalPrincipalPaid += principalPaidThisMonth;
principalBalance -= principalPaidThisMonth;
totalInterestPaid += interestPaidThisMonth;
interestBalance -= interestPaidThisMonth;
loanBalance -= monthlyPayment;
//Add values to JTable
[B]Object[] newRowData = {x, "$" + Currency.format(totalInterestPaid), "$" + Currency.format(loanBalance)};[/B]
[B]model.addRow(newRowData);[/B]
}
[B]tblAmortization.setModel(model);[/B]
}
Edited by Ceryph, 15 August 2011 - 07:46 PM.


Sign In
Create Account

Back to top









