Solved

Mortgage calculator with GUI

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ArrayIndexOutOfBoundException 9 83
GUI builder for Eclipse? 8 36
reverse digits of a number using for loop 5 45
SHA2 certs for IIS AND Java? 2 94
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

803 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