I need help adding tables in the payment area that shows the full payment plan.
Also my months start high to low.
I want the months to start with month 1
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Math;
import java.text.NumberFormat;
import javax.swing.table.*;
//Setup lables and buttons
public class Gmortgage3 extends JFrame implements ActionListener{
private JLabel labelAmount, labelYears, labelInterest, labelMpayment;
private JTextField textAmount, textYears,textInterest,textMpayment;
private JButton buttonCalc, buttonClear, buttonStop;
private GridLayout grid1;
private Container c;
private JLabel labelTopic;
private JTextField textTopic;
private JTextArea area1;
private GridBagLayout grid2;
private Container container;
private GridBagConstraints grid2Constraints;
private JTable jTable1;
public Gmortgage3(){
super("MORTGAGE CALCULATOR");
grid1 = new GridLayout(1,10,5,5); //(rows, columns, vertical space, hortizontal space)
c = getContentPane();
c.setLayout(grid1); //layer the format
//addition of Menu bar
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
//addition of main menu bar item
JMenu calMenu = new JMenu("CALCULATE");
calMenu.setMnemonic('L'); //Hot Key
bar.add(calMenu);
//addition of first item for File
JMenuItem thirtyMenu = new JMenuItem("1-30 Years");
thirtyMenu.setMnemonic('1');
thirtyMenu.addActionListener(
new ActionListener(){ //Listen to the Keyboard
public void actionPerformed(ActionEvent e){ //Set to 30 Years and 5.75%
String S_Amount="200000";
String S_Year = "30";
String S_Percent = "5.75";
textAmount.setText(S_Amount);
textYears.setText(S_Year);
textInterest.setText(S_Percent);
}
}
);
calMenu.add(thirtyMenu);
//addition of second item for File
JMenuItem fifteenMenu = new JMenuItem("2-15 Years");
fifteenMenu.setMnemonic('2');
fifteenMenu.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){ //Set to 15 Years and 5.5%
String S_Amount="200000";
String S_Year = "15";
String S_Percent = "5.5";
textAmount.setText(S_Amount);
textYears.setText(S_Year);
textInterest.setText(S_Percent);
}
}
);
calMenu.add(fifteenMenu);
//addition of second item for File
JMenuItem sevenMenu = new JMenuItem("3-7 Years"); //Set to 7 Years and 5.35%
sevenMenu.setMnemonic('3');
sevenMenu.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
String S_Amount="200000";
String S_Year = "7";
String S_Percent = "5.35";
textAmount.setText(S_Amount);
textYears.setText(S_Year);
textInterest.setText(S_Percent);
}
}
);
calMenu.add(sevenMenu);
//addition of second main menu bar
JMenuItem clearMenu = new JMenuItem("CLEAR");
clearMenu.setMnemonic('R');
clearMenu.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
textMpayment.setText(null);
textInterest.setText(null);
textYears.setText(null);
area1.setText(null);
textAmount.setText(null);//clear program;
}
}
);
bar.add(clearMenu);
//addition of third main menu bar
JMenuItem exitMenu = new JMenuItem("EXIT");
exitMenu.setMnemonic('E');
exitMenu.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
);
bar.add(exitMenu);
//Text Labels
labelAmount = new JLabel(" Amount ");
labelYears = new JLabel(" Years ");
labelInterest = new JLabel(" Interest ");
labelMpayment = new JLabel(" Monthly Payment ");
//Text Boxes 5 wide
textAmount = new JTextField(10);
textYears = new JTextField(2);
textInterest = new JTextField(3);
textMpayment = new JTextField(10);
//Calculate Button
buttonCalc = new JButton("Calculate");
jTable1 = new JTable();
//Add Buttons and Lables
c.add(labelAmount);
c.add(textAmount);
c.add(labelYears);
c.add(textYears);
c.add(labelInterest);
c.add(textInterest);
c.add(labelMpayment);
c.add(textMpayment);
c.add(buttonCalc);
//Action Listeners
textAmount.addActionListener(this);
textYears.addActionListener(this);
textInterest.addActionListener(this);
buttonCalc.addActionListener(this);
//Area Box
container = getContentPane();
grid2 = new GridBagLayout();
container.setLayout(grid2);
grid2Constraints = new GridBagConstraints();
area1 = new JTextArea(" ");
//labelTopic = new JLabel(" Menu Selection");
textTopic = new JTextField(20);
//Area1
grid2Constraints.fill = GridBagConstraints.BOTH;
//grid2Constraints.anchor = GridBagConstraints.CENTER; //2nd row
grid2Constraints.weightx = 120;
grid2Constraints.weighty = 60;
addComponent(area1, 8, 0,9,9);
//additoin of Horizontal Scroll bar to area1 Text Area
Box b = Box.createHorizontalBox();
JScrollPane scrollPane = new JScrollPane(area1);
addComponent ((new JScrollPane(area1)), 8, 0,9,9);
//Size of the Window
setSize (820, 480);
show();
}
public static double Formula (double Amount, double Years, double Interest){
//method to calculate payment
double Interest_Rate = (Interest/1200);
double average = Amount * (Interest_Rate/(1 - Math.pow ((1 + Interest_Rate), -1 * (Years * 12))));
return average;
}
//Stop program
public void Stop(){
System.exit(0);
}
//Fill Area
public void FillTextArea(String myMessage){
area1.append("\t" + myMessage + "\n");
}
//Area Box
private void addComponent(Component c, int row, int col, int width, int height){
grid2Constraints.gridx = col;
grid2Constraints.gridy = row;
grid2Constraints.gridwidth = width;
grid2Constraints.gridheight = height;
grid2.setConstraints(c, grid2Constraints);
container.add(c);
}
public void actionPerformed (ActionEvent evt){
Object source = evt.getSource();
//Mortgage Variables
double Mpayment = 0;
double Interest = 0;
double Amount = 0;
int Years = 0;
double Month = 0;
double Interest_Rate = 0;
double Monthly_Balance = 0;
double Monthly_Principal = 0;
double Monthly_Interest = 0;
//Print Monthly Payment
if (source==buttonCalc){
double rootAmount = Double.parseDouble(textAmount.getText());
double rootYears = Double.parseDouble(textYears.getText());
Month = rootYears * 12;
double rootInterest = Double.parseDouble(textInterest.getText());
Mpayment = Gmortgage3.Formula(rootAmount, rootYears, rootInterest);
String MpaymentString = NumberFormat.getCurrencyInstance().format(Mpayment); //Output monthly payment in $$
textMpayment.setText(MpaymentString);
area1.setText(null);
//Calculate Monthly Payment
double Interest_Rate_2 = (rootInterest/1200);
double Monthly_Payment = rootAmount * (Interest_Rate_2/(1 - Math.pow ((1 + Interest_Rate_2), -1 * (rootYears * 12))));
Monthly_Interest = (Interest_Rate_2 * rootAmount);
Monthly_Principal = (Monthly_Payment - Monthly_Interest);
Monthly_Balance = (rootAmount - Monthly_Principal);
//Print in Area1
area1.append("\t" + "Months to pay on" + "\t" + "Balance" + "\t\t" + "Principal" + "\t\t" + "Interest" +"\n");
//Loop
while (Month > 0){
//Format in Dollar Amount
String MPricipleString = NumberFormat.getCurrencyInstance().format(Monthly_Principal);
String MInterestString = NumberFormat.getCurrencyInstance().format(Monthly_Interest);
String MBalanceString = NumberFormat.getCurrencyInstance().format(Monthly_Balance);
//Monthly Balance
area1.append("\t" + Month + "\t\t" + MBalanceString + "\t\t" + MPricipleString + "\t\t" + MInterestString +"\n");
//De-increment line
Month--;
//(TotalMonth - Month) + 1;
//Calculate New line
Monthly_Interest = (Interest_Rate_2 * Monthly_Balance);
Monthly_Principal = (Monthly_Payment - Monthly_Interest);
Monthly_Balance = (Monthly_Balance - Monthly_Principal);
}
}
}
//Call Gmortgage3
public static void main(String arguments[]){
Gmortgage3 call1 = new Gmortgage3();
call1.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
}
}