?
Solved

Mortgage calculator with GUI

Posted on 2006-07-21
2
Medium Priority
?
1,020 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 2000 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

752 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