11 Replies - 2197 Views - Last Post: 24 April 2012 - 09:31 AM Rate Topic: -----

#1 Zilna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 08-October 10

Mortgage Calculator with GUI and loop

Posted 31 March 2012 - 04:18 PM

I am trying to get a loop to display all payments of the mortgage calculator but keep hitting a brick wall. Here is the code and perhaps someone could point me into the right direction.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mortcalc5;

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author
 * 
 */
public class gui extends JFrame implements ActionListener {

    private JPanel jp = new JPanel();
    private JFrame frame;
    private JLabel lLoan;
    private JLabel lAmount;
    private JLabel lTerm;
    private JTextField tLoan;
    private JTextField tAmount;
    private JButton bSubmit;
    private JButton bReset;
    private JButton bExit;
    private JComboBox duration;
    private Container container;
    private javax.swing.JTextArea output;


    public gui() {
        //create new JFrame container
        frame = new JFrame("McBride Financial Mortgate Calculator");
        container = frame.getContentPane();

        //create layout
        GridBagLayout gbl = new GridBagLayout();

        //set layout on container and handle buttons
        container.setLayout(gbl);

        //place component at cell location (1,1)
        GridBagConstraints gbc = new GridBagConstraints();

        String[] lenloan = {"7 Years" , "15 Years" , "30 Years"};


        lLoan = new JLabel("Please Select Loan Amount");
        lAmount = new JLabel("A Mortgage Payment in the Amount of ");
        lTerm = new JLabel("Length of the Loan");
        tLoan = new JTextField(10);
        tAmount = new JTextField(54);

        tAmount.setEnabled (false);

        duration = new JComboBox(lenloan);
        duration.setSelectedIndex(0);
        bSubmit = new JButton("Calculate Payments");
        bReset = new JButton("Clear");
        bExit = new JButton("Exit");
        gbc.gridx = 0;
        gbc.gridy = 0;
        frame.add(lLoan,gbc);


        gbc.gridx = 1;
        gbc.gridy = 0;
        frame.add(tLoan,gbc);
        gbc.gridx = 0;
        gbc.gridy = 1;
        frame.add(lTerm,gbc);
        gbc.gridx = 1;
        gbc.gridy = 1;
        frame.add(duration,gbc);
        gbc.gridx = 0;
        gbc.gridy = 2;
        frame.add(lAmount,gbc);
        gbc.gridx = 1;
        gbc.gridy = 2;
        frame.add(tAmount,gbc);
        gbc.gridx = 0;
        gbc.gridy = 3;
        frame.add(bSubmit,gbc);
        bSubmit.addActionListener(this);

        gbc.gridx = 1;
        gbc.gridy = 3;
        frame.add(bReset, gbc);
        bReset.addActionListener((ActionListener) this);
        gbc.gridx = 0;
        gbc.gridy = 4;
        frame.add(bExit, gbc);
        bExit.addActionListener((ActionListener) this);

        //show the frame
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }



    public static void main(String[] args) {
        new gui();
    }


    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==bSubmit){
            System.out.println (duration.getSelectedIndex());
            gui();
        }
        else if (e.getSource()==bReset){
            tLoan.setText ("");
            duration.setSelectedIndex(0);
            tAmount.setText("");
        }
        else if (e.getSource()==bExit){
            System.exit(1);
        }

        }


    public void gui() {

        DecimalFormat decFor = new DecimalFormat ("$0.00");
        double loan = Double.parseDouble (tLoan.getText());
        double interest = .0535;
        double interestRate = 5.35;
        double loanTerm = 7;
        double monthTerm = loanTerm*12;

        //loop
        for (double a = monthTerm; a>0; a--){
            double interestPayment = loan*interest;
            double monthPayment = loan/a+interestPayment;

            loan=loan-loan/a;

            output.append("Month" + (monthTerm-a+1) + " Payment: " decFor.format(monthPayment) + "\n");
            output.append("Interest Paid: " + decFor.format(interestPayment)+"\n" );
            output.append("Remaining Balance: " + decFor.format(loan)+"\n\n");
        }


        if(duration.getSelectedIndex()==0){
            interest = .0535;
            interestRate = 5.35;
            loanTerm = 7;

        }
        else if (duration.getSelectedIndex()==1){
            interest = .0550;
            interestRate = 5.50;
            loanTerm = 15;
            
        }
        else if (duration.getSelectedIndex()==2) {
            interest = .0575;
            interestRate = 5.75;
            loanTerm = 30;
            
        }
        double payment = (loan * (interest/12)) / (1-Math.pow(1/(1+(interestRate/12)),(loanTerm*12)));

        String ab = "" + decFor.format (payment);
        tAmount.setText(ab);


    }

}



Is This A Good Question/Topic? 0
  • +

Replies To: Mortgage Calculator with GUI and loop

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Mortgage Calculator with GUI and loop

Posted 31 March 2012 - 04:23 PM

You're currently missing one '+' in this line (I added it):

output.append("Month" + (monthTerm-a+1) + " Payment: " + decFor.format(monthPayment) + "\n");

Line 153. That will get your GUI to display. I don't know what other brick walls you're running into. You'll have to be more specific.
Was This Post Helpful? 1
  • +
  • -

#3 Zilna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 08-October 10

Re: Mortgage Calculator with GUI and loop

Posted 31 March 2012 - 05:05 PM

Thank you for the + sign, I looked at that line 50 times and couldn't figure out the error. Next, when the GUI displays and I enter in the data I get numerous errors and nothing will display. What I am needing is for the display to show all payments for the term of the loan.

Here is the errors I get when I click on the Calculate Payments button :

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at mortcalc5.gui.gui(gui.java:153)
at mortcalc5.gui.actionPerformed(gui.java:123)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6267)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6032)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.window.dispatchEventImpl(window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
0
Was This Post Helpful? 0
  • +
  • -

#4 Zilna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 08-October 10

Re: Mortgage Calculator with GUI and loop

Posted 31 March 2012 - 05:27 PM

If I default back to what I know from previous non-GUI programs and change the code to

System.out.println ("Month" + (monthTerm-a+1) + " Payment: " + decFor.format(monthPayment) + "\n");
            System.out.println ("Interest Paid: " + decFor.format(interestPayment)+"\n" );
            System.out.println ("Remaining Balance: " + decFor.format(loan)+"\n\n");



now my program displays the code in to the box at the bottom of netbeans and not my GUI. I feel as though I got one step closer but now what can I do to get the code to show in the GUI window?
Was This Post Helpful? 0
  • +
  • -

#5 Zilna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 08-October 10

Re: Mortgage Calculator with GUI and loop

Posted 31 March 2012 - 06:11 PM

moving things around and moved the loop to the end and can now get one payment to show in the GUI box

//loop
        for (int a = monthTerm; a>0; a--){
            double interestPayment = loan*interest;
            double monthPayment = loan/a+interestPayment;

            loan = loan-loan/a;

            output.append ("Month " + (monthTerm-a+1) + " Payment: " + decFor.format(monthPayment));
            output.append ("Interest Paid: " + decFor.format(interestPayment));
            output.append ("Remaining Balance: " + decFor.format(loan)+"\n");
        }



What I cannot figure out is how to enlarge the view window in the box and perhaps show all three of these lines that I am asking to be shown. Right now the display is actually only one line in size and needs to be much bigger, but I cannot figure it out.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Mortgage Calculator with GUI and loop

Posted 31 March 2012 - 07:30 PM

NullPointerException at

output.append("Month" + (monthTerm-a+1) + " Payment: " + decFor.format(monthPayment) + "\n");

where did you initialize output ?

class gui... respect Java conventions, a Class name start by a UpperCase letter

public void gui() {

you really have to be masochist to have a method name that match your class name... this is just to confuse every body
Was This Post Helpful? 0
  • +
  • -

#7 Zilna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 08-October 10

Re: Mortgage Calculator with GUI and loop

Posted 01 April 2012 - 04:58 AM

Thank you for trying to put me down, obviously I am newer to this and you are not but is that really a reason to begin to call people rude names?

Masochist - someone who obtains pleasure by receiving punishment

No, this really is not me at all, but hey before you start considering the thought of putting someone down you should begin by stating something more along the lines of "hey, rather than doing it this way perhaps you could try changing this and that around to be more proper" as I am open to suggestions not out right negative comments from someone that would rather be rude to someone asking for help on a forum that is designed for just that.
Was This Post Helpful? 0
  • +
  • -

#8 teQuiero  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 31-March 12

Re: Mortgage Calculator with GUI and loop

Posted 01 April 2012 - 07:31 AM

i mad you a Gui which is cleaner .. just do the math
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class MortgateCaclulator extends JFrame implements ActionListener {
	private JButton btnClear,btnCalPayments,btnExit;
	private JTextField txtAmount,txtMortPayment;
	private Container cn;
	private JPanel pnlOuter,pnltNorth,pnlEast,pnlWest,pnlSouth,pnlCenter;
	private JLabel lblAmount,lblLoan,lblPayment;
	private JComboBox cbbDuration;
	
	
	public MortgateCaclulator(){
		String[] lenloan = {"7 Years" , "15 Years" , "30 Years"};
		
		cn = this.getContentPane();
		pnlOuter = new JPanel(new BorderLayout());
		pnlWest = new JPanel();
		pnlCenter = new JPanel();
		pnlSouth = new JPanel();
		cn.add(pnlOuter);
		//labels
		lblAmount = new JLabel("Please Select Loan Amount");
		lblLoan = new JLabel("Length of the loan");
		lblPayment = new JLabel("A Mortgage Payment in the Amount of");
		//buttons
		btnClear = new JButton("Clear");
		btnCalPayments = new JButton("Calculate Payments");
		btnExit = new JButton("Exit");
		//combobox
		cbbDuration = new JComboBox(lenloan);
		//textfield
		txtAmount = new JTextField();
		txtMortPayment = new JTextField();
	
		pnlOuter.add(pnlWest, BorderLayout.WEST);
		pnlWest.setLayout(new GridLayout(3,2));
		pnlWest.add(lblAmount);
		pnlWest.add(lblLoan);
		pnlWest.add(lblPayment);
		
		pnlOuter.add(pnlCenter, BorderLayout.CENTER);
		pnlCenter.setLayout(new GridLayout(3,2));
		pnlCenter.add(txtAmount);
		pnlCenter.add(cbbDuration);
		pnlCenter.add(txtMortPayment);
		
		pnlOuter.add(pnlSouth, BorderLayout.SOUTH);
		pnlSouth.add(btnClear);
		pnlSouth.add(btnCalPayments);
		pnlSouth.add(btnExit);

	}
	
	public void actionPerformed(ActionEvent e){
		//Todo
	}
	
	public static void main(String[] args){
		MortgateCaclulator calc = new MortgateCaclulator();
		calc.setSize(300,200);
		calc.setVisible(true);
		//setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
}


Was This Post Helpful? 1
  • +
  • -

#9 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9287
  • View blog
  • Posts: 34,808
  • Joined: 12-June 08

Re: Mortgage Calculator with GUI and loop

Posted 01 April 2012 - 09:24 AM

@teQuiero - let's not do people's assignments for them. It's best to work together and learn versus getting spoon fed the code!
Was This Post Helpful? 0
  • +
  • -

#10 teQuiero  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 31-March 12

Re: Mortgage Calculator with GUI and loop

Posted 01 April 2012 - 09:50 AM

View Postmodi123_1, on 01 April 2012 - 09:24 AM, said:

@teQuiero - let's not do people's assignments for them. It's best to work together and learn versus getting spoon fed the code!

oh! sorry about that.. im just new in this group.. i wont do it again..sorry
Was This Post Helpful? 0
  • +
  • -

#11 Zilna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 08-October 10

Re: Mortgage Calculator with GUI and loop

Posted 01 April 2012 - 03:12 PM

I do appreciate the help teQuiero, I did manage to find something else to go with that did everything that I needed. I know that this forum is all about assisting and not actually completing the work for others but thank you for making the attempt. I managed to figure out how to use the JTable to display the payment schedule as needed.
Was This Post Helpful? 0
  • +
  • -

#12 MadhuSharma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-April 12

Re: Mortgage Calculator with GUI and loop

Posted 24 April 2012 - 09:30 AM

View PostteQuiero, on 01 April 2012 - 07:31 AM, said:

i mad you a Gui which is cleaner .. just do the math
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class MortgateCaclulator extends JFrame implements ActionListener {
	private JButton btnClear,btnCalPayments,btnExit;
	private JTextField txtAmount,txtMortPayment;
	private Container cn;
	private JPanel pnlOuter,pnltNorth,pnlEast,pnlWest,pnlSouth,pnlCenter;
	private JLabel lblAmount,lblLoan,lblPayment;
	private JComboBox cbbDuration;
	

Is it same as [url="http://www.loans-portal.com/balloon-mortgage-calculator/ "]balloon mortgage calculator[/url]

	
	public MortgateCaclulator(){
		String[] lenloan = {"7 Years" , "15 Years" , "30 Years"};
		
		cn = this.getContentPane();
		pnlOuter = new JPanel(new BorderLayout());
		pnlWest = new JPanel();
		pnlCenter = new JPanel();
		pnlSouth = new JPanel();
		cn.add(pnlOuter);
		//labels
		lblAmount = new JLabel("Please Select Loan Amount");
		lblLoan = new JLabel("Length of the loan");
		lblPayment = new JLabel("A Mortgage Payment in the Amount of");
		//buttons
		btnClear = new JButton("Clear");
		btnCalPayments = new JButton("Calculate Payments");
		btnExit = new JButton("Exit");
		//combobox
		cbbDuration = new JComboBox(lenloan);
		//textfield
		txtAmount = new JTextField();
		txtMortPayment = new JTextField();
	
		pnlOuter.add(pnlWest, BorderLayout.WEST);
		pnlWest.setLayout(new GridLayout(3,2));
		pnlWest.add(lblAmount);
		pnlWest.add(lblLoan);
		pnlWest.add(lblPayment);
		
		pnlOuter.add(pnlCenter, BorderLayout.CENTER);
		pnlCenter.setLayout(new GridLayout(3,2));
		pnlCenter.add(txtAmount);
		pnlCenter.add(cbbDuration);
		pnlCenter.add(txtMortPayment);
		
		pnlOuter.add(pnlSouth, BorderLayout.SOUTH);
		pnlSouth.add(btnClear);
		pnlSouth.add(btnCalPayments);
		pnlSouth.add(btnExit);

	}
	
	public void actionPerformed(ActionEvent e){
		//Todo
	}
	
	public static void main(String[] args){
		MortgateCaclulator calc = new MortgateCaclulator();
		calc.setSize(300,200);
		calc.setVisible(true);
		//setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1