Solved

Mortgage calculator with GUI

Posted on 2006-07-21
2
1,013 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
  • 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
word0 challenge 4 54
@SBGen Method 3 25
Groovy:unable to resolve class error 2 32
Recommendation vb6 to vb.net or others 14 44
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now