import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
public class MikeSummonsMortgageCalculatorAppWK3 extends JFrame
{
public static void main(String [] args)
{
new MikeSummonsMortgageCalculatorAppWK3();
}
private JButton button1, button2;
private JTextField textName1, textName2;
private JLabel label1, label2;
private JComboBox combo1;
private JTable table;
private JScrollPane scrollPane;
private String columnNames[];
private String dataValues[][];
public MikeSummonsMortgageCalculatorAppWK3()
{
//construct Frame
this.setSize(480,530);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Mike Summons Mortgage Calculator - Week3");
ButtonListener bl = new ButtonListener();
JPanel panel1 = new JPanel();
//panel1.setLayout(new FlowLayout(FlowLayout.LEFT));
//add Loan Amount
label1 = new JLabel("Loan Amount");
//label1.setLayout(new BorderLayout(BorderLayout.NORTH));
panel1.add(label1);
textName1 = new JTextField(15);
//textName1.setLayout(new BorderLayout(BorderLayout.NORTH));
panel1.add(textName1);
//add Loan Interest
combo1 = new JComboBox();
combo1.addItem("7 year at 5.35%");
combo1.addItem("15 year at 5.50%");
combo1.addItem("30 year at 5.75%");
//combo1.setLayout(new BorderLayout(BorderLayout.CENTER));
panel1.add(combo1);
//add Monthly Payment
label2 = new JLabel("Monthly Payment");
//label2.setLayout(new BorderLayout(BorderLayout.SOUTH));
panel1.add(label2);
textName2 = new JTextField(15);
//textName2.setLayout(new BorderLayout(BorderLayout.SOUTH));
panel1.add(textName2);
//add Calc button
button1 = new JButton("Calculate");
button1.addActionListener(bl);
panel1.add(button1);
//add Reset button
button2 = new JButton("Reset");
button2.addActionListener(bl);
panel1.add(button2);
String columnNames[] = {"Loan Payment", "Loan Balance", "Interest Paid"};
double a = 0;
double b = 0;
double c = 0;
String dataValues[][] =
{
{(String.valueOf(a)), (String.valueOf(b)), (String.valueOf(c))}
};
table = new JTable (dataValues, columnNames);
table.setShowHorizontalLines(false);
table.setRowSelectionAllowed(true);
table.setColumnSelectionAllowed(true);
table.setSelectionForeground(Color.white);
table.setSelectionBackground(Color.red);
scrollPane = new JScrollPane(table);
panel1.add(scrollPane, BorderLayout.CENTER);
this.add(panel1);
this.setVisible(true);
}
//add button actions
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == button1)
{
int loanamount = 0;
int[] loanyears;
loanyears = new int[3];
loanyears[0] = 7;
loanyears[1] = 15;
loanyears[2] = 30;
double[] loaninterest;
loaninterest = new double[3];
loaninterest[0] = 5.35;
loaninterest[1] = 5.50;
loaninterest[2] = 5.75;
int i = 0;
NumberFormat cf = NumberFormat.getCurrencyInstance();
String testAmt = textName1.getText();
if (testAmt.length() != 0)
{
loanamount = Integer.parseInt(textName1.getText());
}
else
{
textName1.setText(String.valueOf("Error: No Loan Amount"));
textName1.requestFocus();
}
String testRate = (String)combo1.getSelectedItem();
if (testRate.equals("7 year at 5.35%"))
{
i = 0;
}
else if (testRate.equals("15 year at 5.50%"))
{
i = 1;
}
else if (testRate.equals("30 year at 5.75%"))
{
i = 2;
}
//Declare/Create calculated variables
double monthlyinterest = loaninterest[i] / 100 / 12;
int totalpayments = loanyears[i] * 12;
double monthlypayment = (loanamount * monthlyinterest) / (1 - Math.pow(1 / (1 + monthlyinterest), totalpayments));
//Output calculated monthly payment
textName2.setText(String.valueOf(cf.format(monthlypayment)));
//Create column values
//Table code
for (int n = 0; n < loaninterest.length; n++)
{
double loanbalance;
double loanbalance2;
double interestpaid = loanamount * monthlyinterest;
//* Output loan balance/interest paid
loanbalance = loanamount + (loanamount * monthlyinterest);
while (loanbalance > 0.00)
{
loanbalance2 = loanbalance - monthlypayment;
String dataValues[][] =
{
{(String.valueOf(monthlypayment)), (String.valueOf(loanbalance2)), (String.valueOf(interestpaid))}
};
interestpaid = loanbalance2 * monthlyinterest;
loanbalance = loanbalance2;
loanbalance = loanbalance + (loanbalance * monthlyinterest);
}
//end table code
}
}
else if (e.getSource() == button2)
{
textName1.setText(String.valueOf(""));
textName2.setText(String.valueOf(""));
}
}
}
}
---------- Post added at 03:17 PM ---------- Previous post was at 02:47 PM ----------
To narrow it down, I believe the problem lies with the dataValues variable. I declare its value as 0 to get the table itself up but can not figure out how to change the value of dataValues once the ButtonListener code goes to work.


Sign In
Create Account

Back to top









