Solved

Mortgage calculator with GUI

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to install java on RHEL image on EC2 4 41
runtime exception 2 42
by zero exception 10 52
Problem to Alipay 10 47
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

821 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