We help IT Professionals succeed at work.
Get Started

Mortgage Calculator

Rebnick asked
Last Modified: 2013-11-23
I can not figure out how to get my combo box and arrays working any help would be grealy appreciated. This is what I have so for.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import javax.swing.text.*;
import java.util.Locale;
import javax.swing.JScrollPane;
import javax.swing.JOptionPane;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;

import java.text.*;

public class POS407_W3_RMR extends JPanel
                          implements ActionListener {
    JLabel picture;

    //creating variables

     public double monthly_interest;
     public double monthly_payment;
       public double number_payments;
       public double new_principle;
       public double m_principle;
       public double cm_interest;
       public double mortint;
       double new_balance;

     //Create Labels
       private JLabel ownerLabel;
       private JLabel assignmentLabel;
       private JLabel separatorLabel;
       private JLabel termLabel;
       private JLabel principleLabel;
       private JLabel yearLabel;
       private JLabel monthlyLabel;
       private JLabel fillerLabel;

       //Value for Fields
       double principal;
       double rate;
       int term;
       int pos;

       //Create the text fields and set them up.
       JTextField principalField = new JTextField("100000");
       JTextField termField = new JTextField("7");
       JTextField rateField = new JTextField(".0535");
       JTextField paymentField = new JTextField(10);

     //Create Buttons
       private JButton calculateButton;
       private JButton exitButton;

       //Strings for the labels
       private static String ownerString = "Name : Rebecca Ragucci";
       private static String assignmentString = "Assignment : POS407 Workshop 3";
       private static String separatorString  = "--------------------------------------------------";

     //Create JTextArea and JScrollPane
       JTextArea displayArea = new JTextArea(10,55);
       JScrollPane scroll = new JScrollPane(displayArea);

    public POS407_W3_RMR() {
        super(new BorderLayout());

        String[] petStrings = { "7 years at 5.35", "4years", "30years", "40years", "50years" };
        String termArray [] = {"7","4","30","40","50"};
        String rateArray [] = {"5.35","7.57","5.75","5.65","5.45"};

        //Define Panels
            JPanel headingPane = new JPanel(new GridLayout(0,1));
            JPanel termPane = new JPanel(new GridLayout(0,1));
            JPanel principlePane = new JPanel(new GridLayout(0,1));
            JPanel textPane = new JPanel(new GridLayout(0,2));
            JPanel comboPane = new JPanel(new GridLayout(0,1));

            //Define Labels
            ownerLabel = new JLabel(ownerString);
            assignmentLabel = new JLabel(assignmentString);
            separatorLabel = new JLabel(separatorString);
            principleLabel = new JLabel("Principle: ");
            monthlyLabel = new JLabel("Monthly Payment:$ ");
            fillerLabel = new JLabel("Year / APR%");

            //Define Heading

        //New JPanel
            JPanel actionPane = new JPanel(new GridLayout(0,1));

        //Define Buttons
          calculateButton = new JButton("Calculate");
            exitButton = new JButton("Exit");

        //Create the combo box, select the item at index 4.

        JComboBox comboBox = new JComboBox();

        JComboBox petList = new JComboBox(petStrings);

        //Define Text Pane


        //Lay out


             add(headingPane, BorderLayout.PAGE_START);

         //setting up scroll bars


    /** Listens to the combo box. */
    public void actionPerformed(ActionEvent e)


             DecimalFormat pTwo = new DecimalFormat("0.00");

             Object source = e.getSource();
             if (source == calculateButton)
          //int index = petList.getSelectedIndex();
          //term = termArray[index];
          //rate = rateArray[index];

                //term = Integer.parseInt(termField.getText());
                //principal = Double.parseDouble(principalField.getText());
                  //rate = Double.parseDouble(rateField.getText());
                //principal = 100000.00;
                //term = 7;
                  //rate = .0535;
                        number_payments = (term * 12);

                        rate = (rate / 100);

                        monthly_interest = (rate  / 12);

                        monthly_payment = (principal * monthly_interest) / (1 - Math.pow(1/ (1 + monthly_interest), number_payments));


            new_principle = principal;
            String titles = "Month\t Principal\t\tInterest\t\t\tBalance\n";

            for(int counter = 0; counter <number_payments; counter++)
                        //Scroll Area

                        cm_interest = new_principle * monthly_interest;    //current monthly interest

                    m_principle= monthly_payment - cm_interest ;
                                                                  /* this is your monthly payment minus your monthly interest,
                                                                                       so it is the amount of principal you pay for that month */

                        new_balance = new_principle - m_principle;
                                                                  /*this is the new balance of your principal of your loan*/

                        new_principle = new_balance;

                        mortint = cm_interest * 100;

                        displayArea.append((counter + 1) + ")\t" + pTwo.format(m_principle) + "\t\t" + pTwo.format(mortint)+ "\t\t" + pTwo.format(new_balance) + "\n");


                  else if (source == exitButton)

              JComboBox cb = (JComboBox)e.getSource();
              String petName = (String)cb.getSelectedItem();


    private static void RShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Rebecca'a Mortgage Calc");

        //Create and set up the content pane.
        JComponent newContentPane = new POS407_W3_RMR();
        newContentPane.setOpaque(true); //content panes must be opaque

        //Display the window.

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {

Watch Question
Principal Technologist
This problem has been solved!
Unlock 1 Answer and 15 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE