/*
* Created on 2004¦~2¤ë2¤é
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.text.DecimalFormat;
/**
* @author dcywchan
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class GUILoanTable extends JFrame implements ActionListener {
Object[][] data;
Container container;
JPanel inputPanel,tablePanel;
JTable table;
JScrollPane scrollPane;
TextField loanAmountTextField,
annualInterestRateTextField,
monthlyPaymentTextField;
Button button;
public static void main(String[] args) {
GUILoanTable application = new GUILoanTable();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
public GUILoanTable() {
super( "GUILoanTable" );
loanAmountTextField = new TextField(10);
annualInterestRateTextField = new TextField(20);
monthlyPaymentTextField = new TextField(20);
button = new Button("Calculate");
inputPanel = new JPanel(new GridLayout(1,7));
tablePanel = new JPanel(new GridLayout(1,0));
data = new Object[20][6];
String[] columnNames = {"month",
"startingBalance",
"payment",
"middleBalance",
"interest",
"endingBalance"};
table = new JTable(data, columnNames);
scrollPane = new JScrollPane(table);
DefaultTableCellRenderer rightAligned = new DefaultTableCellRenderer();
rightAligned.setHorizontalAlignment(SwingConstants.RIGHT);
table.getColumn("startingBalance").setCellRenderer(rightAligned);
table.getColumn("startingBalance").setCellRenderer(rightAligned);
table.getColumn("payment").setCellRenderer(rightAligned);
table.getColumn("interest").setCellRenderer(rightAligned);
table.getColumn("endingBalance").setCellRenderer(rightAligned);
inputPanel.add(new Label("Loan Amount"));
inputPanel.add(loanAmountTextField);
inputPanel.add(new Label("Annual Rate"));
inputPanel.add(annualInterestRateTextField);
inputPanel.add(new Label("Monthly Payment"));
inputPanel.add(monthlyPaymentTextField);
inputPanel.add(button);
tablePanel.add(scrollPane);
Container container = getContentPane();
container.add( tablePanel );
container.add( inputPanel, BorderLayout.NORTH);
button.addActionListener(this);
setSize( 600, 300 );
setVisible( true );
}
public void actionPerformed(ActionEvent ae) {
double loanAmount, annualInterestRate, monthlyPayment;
int month;
double startingBalance, payment, middleBalance, interest, endingBalance;
DecimalFormat decimalformat = new DecimalFormat("0.00");
loanAmount = Double.parseDouble(loanAmountTextField.getText());
annualInterestRate = Double.parseDouble(annualInterestRateTextField.getText());
monthlyPayment = Double.parseDouble(monthlyPaymentTextField.getText());
month = 1;
startingBalance = loanAmount;
payment = monthlyPayment;
middleBalance = loanAmount - payment;
interest = middleBalance * annualInterestRate/12/100;
endingBalance = middleBalance + interest;
while ( startingBalance > 0){
data[month-1][0] = new Integer(month);
data[month-1][1] = decimalformat.format(startingBalance);
data[month-1][2] = decimalformat.format(payment);
data[month-1][3] = decimalformat.format(middleBalance);
data[month-1][4] = decimalformat.format(interest);
data[month-1][5] = decimalformat.format(endingBalance);
month++;
startingBalance = endingBalance;
payment = (endingBalance > monthlyPayment?monthlyPayment:endingBalance);
middleBalance = startingBalance - payment;
interest = middleBalance * annualInterestRate/12/100;
endingBalance = middleBalance + interest;
}
for (int i=--month;i<20;i++) {
data[i] = new Object[6];
}
table.repaint();
}
}