I made few changes so have look at it
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.text.DecimalFormat;
public class MorgageCalculator extends JPanel{
//global variable
private JComboBox IntRates;
private JLabel prinlb, rateLb, termLb, paymtLb;
private JButton calBtn, resetBtn;
private JTextField prinTxFld, rateTxFld, termTxFld, monthlyPayment;
private TextArea payAmortTA;
String currentInt;
public MorgageCalculator()
{
String[] comboItems={"5.35","5.50","5.75"};
//currentInt = comboItems [0];
//remove later IntRates = new JComboBox(comboItems);
//creates labels
prinlb = new JLabel("Principal Amount:");
rateLb = new JLabel("Interest Rate:");
termLb = new JLabel("Term:");
paymtLb = new JLabel("Monthly Payment:");
//creates textfields
prinTxFld = new JTextField("",10);
rateTxFld = new JTextField("",5);
rateTxFld.setEditable(false);
termTxFld = new JTextField("",5);
monthlyPayment = new JTextField("",10);
monthlyPayment.setEditable(false);
//combobox pulldown
IntRates = new JComboBox(comboItems);
IntRates.setEditable(false);
/*ComboBox doesn't need it
IntRates.addActionListener(this);*/
payAmortTA = new TextArea(20,50);
//create buttons
calBtn = new JButton("Calculate");
calBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
calculateMortgage();
}
});
resetBtn = new JButton("Reset");
resetBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
prinTxFld.setText("");
rateTxFld.setText("");
termTxFld.setText("");
monthlyPayment.setText("");
payAmortTA.setText ("");
}
});
//add labels and fields
add(prinlb);
add(prinTxFld);
add(rateLb);
add(IntRates);
add(rateTxFld);
add(termLb);
add(termTxFld);
add(paymtLb);
add(monthlyPayment);
add(payAmortTA);
add(calBtn);
add(resetBtn);
}
public void calculateMortgage()
{
//string array to accept which terms to use
rateTxFld.setText(IntRates.getSelectedItem().toString());
double principle=Double.parseDouble (prinTxFld.getText());
double dblrateTxFld=Double.parseDouble(rateTxFld.getText());
if (dblrateTxFld == 5.35)
termTxFld.setText("7");
else if (dblrateTxFld == 5.50)
termTxFld.setText("15");
else if (dblrateTxFld == 5.75)
termTxFld.setText("30");
int inttermTxFld=Integer.parseInt(termTxFld.getText());
// Formats the output of dollar figures
DecimalFormat two_decimal=new DecimalFormat("$0,000.00");
double Payment;
double InterestPaid;
double prinTxFldBal;
double temp;
int Months;
double intMonthlyRate = dblrateTxFld/100;
int MonthTerm = inttermTxFld * 12;
Months=MonthTerm;
//formulas
Payment=(principle * (intMonthlyRate/12)) / (1-1/Math.pow((1+intMonthlyRate/12), MonthTerm));
//converts monthly payment to decimal format
monthlyPayment.setText("" + (two_decimal.format(Payment)));
payAmortTA.append("Month No.\tMonthly Payment\t\tLoan Balance\t\tInterest Payment\n");
for (int counter=0; counter < MonthTerm; counter++)
{
temp=(1-1/Math.pow((1+intMonthlyRate/12), Months));
InterestPaid = Payment * temp;
principle = (principle - Payment + InterestPaid);
MonthTerm = MonthTerm --;
Months--;
payAmortTA.append((counter + 1) + "\t\t" + (two_decimal.format(Payment) + "\t\t" + (two_decimal.format(principle)
+ "\t\t" + (two_decimal.format(InterestPaid) + "\n"))));
}
}
private static void createAndShowGui()
{
JFrame frame = new JFrame("Mortgage Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MorgageCalculator calculator = new MorgageCalculator();
calculator.setOpaque(true);
frame.setContentPane(calculator);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
createAndShowGui();
}
}
);
}
} Few recommendations you should use some layout
before calculation you should run check() method that will check if the fields been fill in (left for you to implement)
start follow naming convention class name start with capital letter any any other world should starts with capital too(example MorgageCalculator), methods names starts with lower letter but every new word starts with capital (example calculateMorgage() ), variable names same as method names just in case of constants you should use all capitals words separated by underscore (example INTEREST_RATE)
next time when you post code please use code tags [code]YOUR CODE HERE[/code] or [code=Java]YOUR CODE HERE[/code]
peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902