Java JFrame create mortgage calculator

Description

Java JFrame create mortgage calculator

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;

import javax.swing.BorderFactory;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**/*from   w  w w . ja v a2s.c  o m*/
 * It implements a mortgage calculator that uses four JFormattedTextFields.
 */
class MyPanel extends JPanel implements PropertyChangeListener {
   // Values for the fields
   private double amount = 134000;
   private double rate = 6.5; // 6.5%
   private int numPeriods = 25;

   // Labels to identify the fields
   private JLabel amountLabel;
   private JLabel rateLabel;
   private JLabel numPeriodsLabel;
   private JLabel paymentLabel;

   // Strings for the labels
   private static String amountString = "Loan Amount: ";
   private static String rateString = "APR (%): ";
   private static String numPeriodsString = "Years: ";
   private static String paymentString = "Monthly Payment: ";

   // Fields for data entry
   private JFormattedTextField amountField;
   private JFormattedTextField rateField;
   private JFormattedTextField numPeriodsField;
   private JFormattedTextField paymentField;

   // Formats to format and parse numbers
   private NumberFormat amountFormat;
   private NumberFormat percentFormat;
   private NumberFormat paymentFormat;

   public MyPanel() {
      super(new BorderLayout());
      amountFormat = NumberFormat.getNumberInstance();

      percentFormat = NumberFormat.getNumberInstance();
      percentFormat.setMinimumFractionDigits(3);

      paymentFormat = NumberFormat.getCurrencyInstance();
      double payment = computePayment(amount, rate, numPeriods);

      amountLabel = new JLabel(amountString);
      rateLabel = new JLabel(rateString);
      numPeriodsLabel = new JLabel(numPeriodsString);
      paymentLabel = new JLabel(paymentString);

      // Create the text fields and set them up.
      amountField = new JFormattedTextField(amountFormat);
      amountField.setValue(new Double(amount));
      amountField.setColumns(10);
      amountField.addPropertyChangeListener("value", this);

      rateField = new JFormattedTextField(percentFormat);
      rateField.setValue(new Double(rate));
      rateField.setColumns(10);
      rateField.addPropertyChangeListener("value", this);

      numPeriodsField = new JFormattedTextField();
      numPeriodsField.setValue(new Integer(numPeriods));
      numPeriodsField.setColumns(10);
      numPeriodsField.addPropertyChangeListener("value", this);

      paymentField = new JFormattedTextField(paymentFormat);
      paymentField.setValue(new Double(payment));
      paymentField.setColumns(10);
      paymentField.setEditable(false);
      paymentField.setForeground(Color.red);

      amountLabel.setLabelFor(amountField);
      rateLabel.setLabelFor(rateField);
      numPeriodsLabel.setLabelFor(numPeriodsField);
      paymentLabel.setLabelFor(paymentField);

      // Lay out the labels in a panel.
      JPanel labelPane = new JPanel(new GridLayout(0, 1));
      labelPane.add(amountLabel);
      labelPane.add(rateLabel);
      labelPane.add(numPeriodsLabel);
      labelPane.add(paymentLabel);

      JPanel fieldPane = new JPanel(new GridLayout(0, 1));
      fieldPane.add(amountField);
      fieldPane.add(rateField);
      fieldPane.add(numPeriodsField);
      fieldPane.add(paymentField);

      setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
      add(labelPane, BorderLayout.CENTER);
      add(fieldPane, BorderLayout.LINE_END);
   }

   public void propertyChange(PropertyChangeEvent e) {
      Object source = e.getSource();
      if (source == amountField) {
         amount = ((Number) amountField.getValue()).doubleValue();
      } else if (source == rateField) {
         rate = ((Number) rateField.getValue()).doubleValue();
      } else if (source == numPeriodsField) {
         numPeriods = ((Number) numPeriodsField.getValue()).intValue();
      }

      double payment = computePayment(amount, rate, numPeriods);
      paymentField.setValue(new Double(payment));
   }

   double computePayment(double loanAmt, double rate, int numPeriods) {
      double I, partial1, denominator, answer;

      numPeriods *= 12; // get number of months
      if (rate > 0.01) {
         I = rate / 100.0 / 12.0; // get monthly rate from annual
         partial1 = Math.pow((1 + I), (0.0 - numPeriods));
         denominator = (1 - partial1) / I;
      } else { 
         denominator = numPeriods;
      }

      answer = (-1 * loanAmt) / denominator;
      return answer;
   }
}

public class Main {
   public static void main(final String args[]) throws Exception {
      JFrame frame = new JFrame("Formatted Example");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      frame.add(new MyPanel(), BorderLayout.CENTER);

      frame.pack();
      frame.setVisible(true);
   }
}



PreviousNext

Related