TextArea Data
I'm trying to get a mortgage calculator that allows me to calculate the data, add it to a table, and show an amortization for that particular mortgage. I can do everything except for get the amortization information to print to the textArea section. I've split it up into three panes in the same window. One for the mortgage buttons and fields, one for the table, and a textArea for the amortization. Below are snipits from my code. Any help on how to get the information to print to the textArea? It compiles and runs fine; but gives strange errors in the command window :
C:\Programs\Java\Other>java MortgageGUI
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: addin
g container's parent to itself
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at MortgageGUI.Amortization(MortgageGUI.java:288)
at MortgageGUI.actionPerformed(MortgageGUI.java:179)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
... etc ...
Here is my code :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.NumberFormat;
import java.util.*;
////////////////////////// SETUP GUI DATA //////////////////////////
publicclass MortgageGUIextends JFrameimplements ActionListener{
//... variables ...
privatedouble amount, term, rate, payment;
private JTextArea amorText;
private JPanel amortizationData =new JPanel();
//... variables ...
////////////////////////// DISPLAY GUI //////////////////////////
public MortgageGUI(){
//... setup stuff ...
createMortgageData();// Create the panels
createTableData();
createAmortization();
//... split pane setup ...
////////////////////////// CREATE MORTGAGE //////////////////////////
publicvoid createMortgageData(){
//... code ...
//... buttons and fields ...
btnAmortization.addActionListener(this);
mortgageData.add(btnAmortization);
//... etc ...
////////////////////////// CREATE AMORTIZATION //////////////////////////
publicvoid createAmortization(){
dataValues[i][0] = amountF.getText();// Create data
dataValues[i][1] = termF.getText();
dataValues[i][2] = rateF.getText();
dataValues[i][3] = paymentF.getText();
table =new JTable( dataValues, columnNames );// Create a new table instance
scrollPane =new JScrollPane(table);// Add the table to a scrolling pane
tableData.add(scrollPane);
setSize(600, 600);// Size for GUI is set
show();
i++;
}
////////////////////////// CREATE TABLE //////////////////////////
publicvoid createTableData(){
//... code ...
////////////////////////// ACTION //////////////////////////
publicvoid actionPerformed(ActionEvent evt){
//... actions for buttons ...
elseif (src == btnAmortization){
Amortization(amount, term, rate);
}
////////////////////////// AMORTIZATION OUTPUT //////////////////////////
publicvoid Amortization(double amount,double term,double rate){
amorText =new JTextArea();// Create a new table instance
scrollPane =new JScrollPane(amortizationData);// Add the table to a scrolling pane
amortizationData.add(scrollPane, BorderLayout.NORTH);
NumberFormat cf = NumberFormat.getCurrencyInstance(Locale.US);// Allows utilization of US currency format
int month=1;// Variable to track which month number payment is at.
double months=(term*12);// Initialization of term in months using 'years"
double MonInt=(rate/(12*100));// Monthly interest rate calculated
double MonPayment=(amount*MonInt)/(1-(Math.pow((1/(1+MonInt)), months)));// Monthly payments calculated
amorText.setText("\n\nMonth\tPayment\t\tInterest\tPrinciple\tRemaining");
amorText.setText("===================================================================");// For divider
while (amount>0){// Loop for outputting payments
amorText.append(month+"\t"+cf.format(MonPayment)+
"\t"+cf.format((amount*MonInt))+"\t\t"+cf.format(MonPayment-(amount*MonInt)));// Output of mortgage data
amount=(amount-(MonPayment-(amount*MonInt)));// Decreases amount left to pay for loan
if (MonPayment-(amount*MonInt)<1002){ System.out.print("\t");}// Enters extra tab when needed to display properly
amorText.setText("\t"+cf.format(amount));// Prints the amount left to pay for next month
month++;// Increment month
if (amount<=MonPayment){break;}// Exits loop when no more payments need to be made
}// End loop for outputting payments
amount=(amount-(MonPayment-(amount*MonInt)));// Decrements amount remaining for final payment
}
// ... code ...

