Solved

# Mortgage calculator doesn't display amortization table

Posted on 2006-06-10
Medium Priority
210 Views
My program compiles and calculates the payment based on the user selected radio button. I added a button to execute and populate a scroll box with several values as indicated by the headings in the box. My problem is, nothing displays when I push the display table button although I created a statement to append the output. The code I expected to do this can be ound after the comment  // method called when user clicks tableJButton.
Any assistance would be appreciated.
Thanks,
Jon

My program is below.

//Week2 Jan Schab

/* Mortgage Monyhly Payment Calculator with graphical user interface
Programmer:  J.Schab
Date:        June 4,2006
Filename:    Mortgage.java
Purpose:     This project accepts the mortgage amount, the annual interest
rate, and the term of the loan in years from the user. The program
then calculates the monthly mortgage payment and displays it in currency format.
There is a quit button to exit the application, and a clear button to
clear user entered fields in order to calculate a new mortgage. The program
will also clear the payment field should the user press any key while positioned
in any input textfield.

*/

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.JScrollPane.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.text.NumberFormat;
import java.util.Locale;
import java.text.*;
import java.text.DecimalFormat;

class Mortgage extends JFrame
{

//Program variables

double yearlyRate[] =
{
// array of interest rates
5.35, 5.50, 5.75
};
int term[] =
{
//array of terms
7, 15, 30
};
int termMonths[] =
{
//array of terms in months
84, 180, 360
};

int i = 0;
int payment = 0; // payment counte
double loanbalance = 0.00;
double monthlyPrinciplePaid = 0.00;
double monthlyInterest = 0.00;
double loanBalance = 0.00;

Border rowborder = new EmptyBorder( 3, 10, 3, 10 );
String titles = "Month \t Principal\t Interest\tBalance\n";

// JLabel and JTextField for the mortgage amount
private JLabel amountJLabel;
private JTextField amountJTextField;

// JLabel for monthly mortgage payment
private JLabel monthpayJLabel;

// JTextArea for displaying payment
private JTextArea outputJTextArea;

// JButton to initiate calculations
private JButton calculateJButton;

// JButton to display amortization table
private JButton tableJButton;

// JButton to clear entries
private JButton clearJButton;

// JButton to quit
private JButton quitJButton;

JTextArea displayArea = new JTextArea(10, 45);
JScrollPane scroll = new JScrollPane(displayArea);

// no-argument constructor
public Mortgage()

{
createUserInterface();
}

// create and position the GUI components; register event handlers
private void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();

//JTextArea displayArea = new JTextArea(10, 45);
//JScrollPane scroll = new JScrollPane(displayArea);

setTitle("Mortgage Calculator"); // set title bar text

// set northLabel using borderlayout
headerJLabel.setText("Enter a mortgage amount and select a loan");

// enable specific positioning of GUI components
contentPane.setLayout(null);

// set up amountJLabel
amountJLabel = new JLabel();
amountJLabel.setBounds(76, 36, 104, 26);
amountJLabel.setText("Mortgage amount:");

// set up amountJTextField
amountJTextField = new JTextField();
amountJTextField.setBounds(200, 36, 56, 26);
amountJTextField.setHorizontalAlignment(JTextField.RIGHT);

new KeyAdapter() // anonymous inner class
{
// event handler for key pressed in amountJTextField
public void keyPressed(KeyEvent event)
{
amountJTextFieldkeyPressed(event);
}

} // end anonymous inner class

); // end call to addKeyListener

ButtonGroup bgroup = new ButtonGroup();
radioPanel.setBounds( 90, 56, 195, 95 );

// set up calculateJButton
calculateJButton = new JButton();
calculateJButton.setBounds(26, 320, 90, 26);
calculateJButton.setText("Calculate");

new ActionListener() // anonymous inner class
{
// event handler called when calculateJButton is pressed
public void actionPerformed(ActionEvent event)
{
calculateJButtonActionPerformed(event);
}

} // end anonymous inner class

); // end call to addActionListener

// set up tableJButton
tableJButton = new JButton();
tableJButton.setBounds(10, 166, 116, 26);
tableJButton.setText("Display Table");

new ActionListener() // anonymous inner class
{
// event handler called when calculateJButton is pressed
public void actionPerformed(ActionEvent event)
{
calculateJButtonActionPerformed(event);
}

} // end anonymous inner class

); // end call to addActionListener

// set up clearJButton
clearJButton = new JButton();
clearJButton.setBounds(136, 320, 90, 26);
clearJButton.setText("Clear");

new ActionListener() // anonymous inner class
{
// event handler called when clearJButton is pressed
public void actionPerformed(ActionEvent event)
{
clearJButtonActionPerformed(event);
}

} // end anonymous inner class

); // end call to addActionListener

// set up quitJButton
quitJButton = new JButton();
quitJButton.setBounds(250, 320, 90, 26);
quitJButton.setText("Quit");

new ActionListener() // anonymous inner class
{
// event handler called when quitJButton is pressed
public void actionPerformed(ActionEvent event)
{
quitJButtonActionPerformed(event);
}

} // end anonymous inner class

); // end call to addActionListener

// set up outputJTextArea
outputJTextArea = new JTextArea();
outputJTextArea.setBounds(200, 146, 56, 26);
outputJTextArea.setEditable(false);

//set up monthpayJLabel
monthpayJLabel = new JLabel();
monthpayJLabel.setBounds(76, 146, 156, 26);
monthpayJLabel.setText("Monthly Payment:");

scroll.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
scroll.setBounds(20, 186, 356, 130);
setVisible(true);
displayArea.setText(titles);

} // end method createUserInterface

// called when user presses key in amountJTextField
private void amountJTextFieldkeyPressed(KeyEvent event)
{
outputJTextArea.setText(""); // clear outputJTextArea
}

// method called when user clicks calculateJButton
private void calculateJButtonActionPerformed(ActionEvent event)
{
try
{
if (aButton.isSelected()){
i = 0;
}

if (bButton.isSelected()){
i = 1;
}

if (cButton.isSelected()){
i = 2;
}

int monthCount = (term[i] * 12); // Calculate months from years
int amount = Integer.parseInt(amountJTextField.getText());
double interestRateMonthly = (yearlyRate[i] / 12) / 100;
// Calculate monthly interest
double monthlyPayment; // monthly payment amount variable

// Calculate monthly payment
monthlyPayment = (amount * interestRateMonthly) / (1-Math.pow(1 +
interestRateMonthly,  - monthCount));

DecimalFormat currency = new DecimalFormat("\$0.00");
// Format output
outputJTextArea.setText(currency.format(monthlyPayment));
// Output payment amount

}
catch (NumberFormatException ex){}
}

// method called when user clicks tableJButton
private void tableJButtonActionPerformed(ActionEvent event)
{
try
{
if (aButton.isSelected()){
i = 0;
}

if (bButton.isSelected()){
i = 1;
}

if (cButton.isSelected()){
i = 2;
}

//*For loop to calculate payments
for (int a = 0; a < termMonths[i]; a = a + 1)
{

int monthCount = (term[i] * 12); // Calculate months from years
double amount = Integer.parseInt(amountJTextField.getText());
double monthlyPayment; // monthly payment amount variable
double interestRateMonthly = (yearlyRate[i] / 12) / 100;
// Calculate monthly interest
// Calculate monthly payment
monthlyPayment = (amount * interestRateMonthly) / (1-Math.pow(1 +
interestRateMonthly,  - monthCount));
payment++; //increment payment counter
// Calculate monthly interest paid
monthlyInterest = amount * interestRateMonthly;
// Calculate monthly principle paid
monthlyPrinciplePaid = monthlyPayment - monthlyInterest;
//Calculate loan balance
loanBalance = amount - monthlyPrinciplePaid;

amount = loanBalance; // assign new loan balance to principle

displayArea.append(payment+"\t\t"
+monthlyPrinciplePaid+"\t\t"
+monthlyInterest+"\t\t"
+loanBalance+"\n");
//paymentNumber++;
displayArea.setCaretPosition(0);

}

}
catch (NumberFormatException ex){}
}

// method called when user clicks clearJButton
private void clearJButtonActionPerformed(ActionEvent event)
{
try
{
outputJTextArea.setText(""); // clear outputJTextArea
amountJTextField.setText(""); // clear amountJTextField
//interestJTextField.setText(""); // clear interestJTextField
//termJTextField.setText(""); // clear termJTextField

}
catch (NumberFormatException ex){}
}

// method called when user clicks quitJButton
private void quitJButtonActionPerformed(ActionEvent event)
{
try
{
System.exit(0);

}
catch (NumberFormatException ex){}
}

// main method
public static void main(String[]args)
{
Mortgage application = new Mortgage();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.setSize(400, 400);
application.setVisible(true);
} // end method main

} // end class Mortgage
0
Question by:ghost8067
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 14

Accepted Solution

hoomanv earned 2000 total points
ID: 16877943
you forgot to add action listener to tableJButton
mistakenly you have added ActionListener to calculateJButton two times
0

Author Comment

ID: 16878160
Thanks, I was looking at it for hour and didn't see it. I need some formatting work, but it is working now.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
###### Suggested Courses
Course of the Month15 days, 4 hours left to enroll