Solved

Mortgage calculator with GUI

Posted on 2006-07-21
2
1,019 Views
Last Modified: 2013-11-23
This is a Java program to allow user to input loan amount, loan term, and interest rate. I am unable to display the payments after pressing the Calculate button. There seems to be a problem with the setText.

/*
     This program will use a GUI to ask the user for input.
     It will calculate the payment for a mortgage.
     Version 1.0
*/

   import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;
   import java.io.*;
   import java.text.DecimalFormat;
   import javax.swing.JTextField.*;

   import java.text.NumberFormat;

    public class MortgagePay extends JFrame implements ActionListener
   {
          //setup format for currency
          NumberFormat money = NumberFormat.getCurrencyInstance();


          //Declare output stream
          DataOutputStream output;

          //Construct a panel for each row
          JPanel firstRow = new JPanel();
          JPanel secondRow = new JPanel();
          JPanel thirdRow = new JPanel();
          JPanel fourthRow = new JPanel();

          //Construct a panel for the fields and buttons
          JPanel fieldPanel = new JPanel();
          JPanel buttonPanel = new JPanel();

          //Construct labels and text boxes
          JLabel loanLabel = new JLabel("Mortgage Amount:          ");
               JTextField p  = new JTextField(15);
          JLabel termLabel = new JLabel("What is the Term (in years):");
               JTextField t = new JTextField(5);
          JLabel rateLabel = new JLabel("What is the Rate:");
               JTextField r = new JTextField(5);
          JLabel paymentLabel = new JLabel("Your Payment will be:");
               JLabel displayPayment = new JLabel();

          //Construct Button
          JButton calculateButton = new JButton("Calculate Payment");


       public static void main(String[] args)
      {

      //Set look and feel for the interface
         try
         {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
         }
             catch (Exception e)
            {
               JOptionPane.showMessageDialog(null, "The UIManager could not set the Look and Feel for this application.",
                  "Error", JOptionPane.INFORMATION_MESSAGE);
            }
         MortgagePay f = new MortgagePay();

         f.setSize(450,350);
         f.setTitle("Mortgage Payment Calculator");
         f.setResizable(false);
         f.setLocation(200,200);
         f.setVisible(true);
      }

       public MortgagePay()
      {
          //Bulid Container
          Container c = getContentPane();
          c.setLayout((new BorderLayout()));
          fieldPanel.setLayout(new GridLayout(8,1));
          FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,5,3);
               firstRow.setLayout(rowSetup);
               secondRow.setLayout(rowSetup);
               thirdRow.setLayout(rowSetup);
               fourthRow.setLayout(rowSetup);
          buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));


          //add fields to rows
          firstRow.add(loanLabel);
          firstRow.add(p);
          secondRow.add(termLabel);
          secondRow.add(t);
          thirdRow.add(rateLabel);
          thirdRow.add(r);
          fourthRow.add(paymentLabel);
          fourthRow.add(displayPayment);

          //Add rows to panel
          fieldPanel.add(firstRow);
          fieldPanel.add(secondRow);
          fieldPanel.add(thirdRow);
          fieldPanel.add(fourthRow);
          //add button to panel
          buttonPanel.add(calculateButton);

          //Add panels to frame
          c.add(fieldPanel, BorderLayout.CENTER);
          c.add(buttonPanel, BorderLayout.SOUTH);

          //add fuctionality to the button
          calculateButton.addActionListener(this);

     }

       public void actionPerformed(ActionEvent e)
      {

               //This is going to call the method for doing the math
               mortCal();

       }

                 public void mortCal()
               {
               double principle = Double.parseDouble(p.getText());
               double rate = Double.parseDouble(r.getText());
               double term = Double.parseDouble(t.getText());
               double interest = (rate/100/12);
               double payment = (principle*interest)/(1-Math.pow(1/(1+interest), term*12));
               String displayPayment = money.format(payment);
               displayPayment.setText(payment);
               }
   }
0
Comment
Question by:jjbraxton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 5

Accepted Solution

by:
tbboyett earned 500 total points
ID: 17158230
replace displayPayment.setText(payment); with the following

paymentLabel.setText("" + payment);
0
 
LVL 5

Expert Comment

by:tbboyett
ID: 17158234
You were trying to settext to the string and not the label
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question