• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

Mortgage calculator doesn't display amortization table

My program compiles and calculates the payment based on the user selected radio button. I added a button to execute and populate a scroll box with several values as indicated by the headings in the box. My problem is, nothing displays when I push the display table button although I created a statement to append the output. The code I expected to do this can be ound after the comment  // method called when user clicks tableJButton.
Any assistance would be appreciated.
Thanks,
Jon

My program is below.

//Week2 Jan Schab


/* Mortgage Monyhly Payment Calculator with graphical user interface
Programmer:  J.Schab
Date:        June 4,2006
Filename:    Mortgage.java
Purpose:     This project accepts the mortgage amount, the annual interest
rate, and the term of the loan in years from the user. The program
then calculates the monthly mortgage payment and displays it in currency format.
There is a quit button to exit the application, and a clear button to
clear user entered fields in order to calculate a new mortgage. The program
will also clear the payment field should the user press any key while positioned
in any input textfield.


 */



import javax.swing.*;
import javax.swing.border.*;
import javax.swing.JScrollPane.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.text.NumberFormat;
import java.util.Locale;
import java.text.*;
import java.text.DecimalFormat;



class Mortgage extends JFrame
{


     //Program variables

        double yearlyRate[] =
        {
            // array of interest rates
            5.35, 5.50, 5.75
        };
        int term[] =
        {
            //array of terms
            7, 15, 30
        };
        int termMonths[] =
        {
            //array of terms in months
            84, 180, 360
        };

        int i = 0;
        int payment = 0; // payment counte
        double loanbalance = 0.00;
        double monthlyPrinciplePaid = 0.00;
        double monthlyInterest = 0.00;
        double loanBalance = 0.00;

    private JLabel headerJLabel;

    Border rowborder = new EmptyBorder( 3, 10, 3, 10 );
    String titles = "Month \t Principal\t Interest\tBalance\n";        

    // JLabel and JTextField for the mortgage amount
    private JLabel amountJLabel;
    private JTextField amountJTextField;

    // JLabel for monthly mortgage payment
    private JLabel monthpayJLabel;

    // JTextArea for displaying payment
    private JTextArea outputJTextArea;

   
    // JButton to initiate calculations
    private JButton calculateJButton;

    // JButton to display amortization table
    private JButton tableJButton;

    // JButton to clear entries
    private JButton clearJButton;

    // JButton to quit
    private JButton quitJButton;

    JPanel radioPanel = new JPanel();
    JRadioButton aButton = new JRadioButton("7 Years at 5.35%" , true);
    JRadioButton bButton = new JRadioButton("15 Years at 5.50%" , false);
    JRadioButton cButton = new JRadioButton("30 Years at 5.75%", false);

    JTextArea displayArea = new JTextArea(10, 45);
        JScrollPane scroll = new JScrollPane(displayArea);


   

    // no-argument constructor    
    public Mortgage()

     
   
    {
        createUserInterface();
    }

    // create and position the GUI components; register event handlers
    private void createUserInterface()
    {
        // get content pane for attaching GUI components
        Container contentPane = getContentPane();

        //JTextArea displayArea = new JTextArea(10, 45);
        //JScrollPane scroll = new JScrollPane(displayArea);

       
        setTitle("Mortgage Calculator"); // set title bar text


        // set northLabel using borderlayout
        headerJLabel = new JLabel();
        headerJLabel.setBounds(10, 1, 300, 46);
        headerJLabel.setText("Enter a mortgage amount and select a loan");
        contentPane.add(headerJLabel);

        // enable specific positioning of GUI components
        contentPane.setLayout(null);

        // set up amountJLabel
        amountJLabel = new JLabel();
        amountJLabel.setBounds(76, 36, 104, 26);
        amountJLabel.setText("Mortgage amount:");
        contentPane.add(amountJLabel);

        // set up amountJTextField
        amountJTextField = new JTextField();
        amountJTextField.setBounds(200, 36, 56, 26);
        amountJTextField.setHorizontalAlignment(JTextField.RIGHT);
        contentPane.add(amountJTextField);
        amountJTextField.addKeyListener(

        new KeyAdapter() // anonymous inner class
        {
            // event handler for key pressed in amountJTextField
            public void keyPressed(KeyEvent event)
            {
                amountJTextFieldkeyPressed(event);
            }

        } // end anonymous inner class

        ); // end call to addKeyListener

    // added code below for radio buttons

        //JPanel radioPanel = new JPanel();
        //       JRadioButton aButton = new JRadioButton("7 Years at 5.35%" , true);
        //       JRadioButton bButton = new JRadioButton("15 Years at 5.50%" , false);
        //      JRadioButton cButton = new JRadioButton("30 Years at 5.75%", false);


        ButtonGroup bgroup = new ButtonGroup();      
          bgroup.add(aButton);
          bgroup.add(bButton);
          bgroup.add(cButton);
          radioPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 4, 4 ));      
          radioPanel.add(aButton);
          radioPanel.add(bButton);
          radioPanel.add(cButton);
          contentPane.add(radioPanel);
          //radioPanel.setMaximumSize( new Dimension( 10000, radioPanel.getMinimumSize().height ) );
          radioPanel.setBorder( rowborder );
          radioPanel.setBounds( 90, 56, 195, 95 );
         
               
         
        // set up calculateJButton
        calculateJButton = new JButton();
        calculateJButton.setBounds(26, 320, 90, 26);
        calculateJButton.setText("Calculate");
        contentPane.add(calculateJButton);
        calculateJButton.addActionListener(

        new ActionListener() // anonymous inner class
        {
            // event handler called when calculateJButton is pressed
            public void actionPerformed(ActionEvent event)
            {
                calculateJButtonActionPerformed(event);
            }

        } // end anonymous inner class

        ); // end call to addActionListener

        // set up tableJButton
        tableJButton = new JButton();
        tableJButton.setBounds(10, 166, 116, 26);
        tableJButton.setText("Display Table");
        contentPane.add(tableJButton);
        calculateJButton.addActionListener(

        new ActionListener() // anonymous inner class
        {
            // event handler called when calculateJButton is pressed
            public void actionPerformed(ActionEvent event)
            {
                calculateJButtonActionPerformed(event);
            }

        } // end anonymous inner class

        ); // end call to addActionListener


        // set up clearJButton
        clearJButton = new JButton();
        clearJButton.setBounds(136, 320, 90, 26);
        clearJButton.setText("Clear");
        contentPane.add(clearJButton);
        clearJButton.addActionListener(

        new ActionListener() // anonymous inner class
        {
            // event handler called when clearJButton is pressed
            public void actionPerformed(ActionEvent event)
            {
                clearJButtonActionPerformed(event);
            }

        } // end anonymous inner class

        ); // end call to addActionListener


        // set up quitJButton
        quitJButton = new JButton();
        quitJButton.setBounds(250, 320, 90, 26);
        quitJButton.setText("Quit");
        contentPane.add(quitJButton);
        quitJButton.addActionListener(

        new ActionListener() // anonymous inner class
        {
            // event handler called when quitJButton is pressed
            public void actionPerformed(ActionEvent event)
            {
                quitJButtonActionPerformed(event);
            }

        } // end anonymous inner class

        ); // end call to addActionListener


        // set up outputJTextArea
        outputJTextArea = new JTextArea();
        contentPane.add(outputJTextArea);
        outputJTextArea.setBounds(200, 146, 56, 26);
        outputJTextArea.setEditable(false);

        //set up monthpayJLabel
        monthpayJLabel = new JLabel();
        monthpayJLabel.setBounds(76, 146, 156, 26);
        monthpayJLabel.setText("Monthly Payment:");
        contentPane.add(monthpayJLabel);

        scroll.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        contentPane.add(scroll);
        scroll.setBounds(20, 186, 356, 130);
        setVisible(true);
        displayArea.setText(titles);
         

       
              
    } // end method createUserInterface

    // called when user presses key in amountJTextField
    private void amountJTextFieldkeyPressed(KeyEvent event)
    {
        outputJTextArea.setText(""); // clear outputJTextArea
    }

        // method called when user clicks calculateJButton
    private void calculateJButtonActionPerformed(ActionEvent event)
    {
        try
        {
            if (aButton.isSelected()){
             i = 0;
          }
         
         
          if (bButton.isSelected()){
             i = 1;
          }
         
         
          if (cButton.isSelected()){
             i = 2;
          }


           
            int monthCount = (term[i] * 12); // Calculate months from years
            int amount = Integer.parseInt(amountJTextField.getText());
            double interestRateMonthly = (yearlyRate[i] / 12) / 100;
                // Calculate monthly interest
            double monthlyPayment; // monthly payment amount variable

            // Calculate monthly payment
            monthlyPayment = (amount * interestRateMonthly) / (1-Math.pow(1 +
                interestRateMonthly,  - monthCount));



            DecimalFormat currency = new DecimalFormat("$0.00");
                // Format output
                  outputJTextArea.setText(currency.format(monthlyPayment));
                // Output payment amount

        }
        catch (NumberFormatException ex){}
    }

    // method called when user clicks tableJButton
    private void tableJButtonActionPerformed(ActionEvent event)
    {
        try
        {
            if (aButton.isSelected()){
             i = 0;
          }
         
         
          if (bButton.isSelected()){
             i = 1;
          }
         
         
          if (cButton.isSelected()){
             i = 2;
          }
           
            //*For loop to calculate payments
            for (int a = 0; a < termMonths[i]; a = a + 1)
            {

                int monthCount = (term[i] * 12); // Calculate months from years
                double amount = Integer.parseInt(amountJTextField.getText());
                double monthlyPayment; // monthly payment amount variable
                double interestRateMonthly = (yearlyRate[i] / 12) / 100;
                // Calculate monthly interest
                // Calculate monthly payment
                monthlyPayment = (amount * interestRateMonthly) / (1-Math.pow(1 +
                interestRateMonthly,  - monthCount));
                payment++; //increment payment counter
                // Calculate monthly interest paid
                monthlyInterest = amount * interestRateMonthly;
                // Calculate monthly principle paid
                monthlyPrinciplePaid = monthlyPayment - monthlyInterest;
                //Calculate loan balance
                loanBalance = amount - monthlyPrinciplePaid;

                amount = loanBalance; // assign new loan balance to principle


                 displayArea.append(payment+"\t\t"
                                       +monthlyPrinciplePaid+"\t\t"
                                       +monthlyInterest+"\t\t"
                                       +loanBalance+"\n");
                        //paymentNumber++;
                        displayArea.setCaretPosition(0);

               

               

            }
       
        }
        catch (NumberFormatException ex){}
    }

    // method called when user clicks clearJButton
    private void clearJButtonActionPerformed(ActionEvent event)
    {
        try
        {
            outputJTextArea.setText(""); // clear outputJTextArea
            amountJTextField.setText(""); // clear amountJTextField
            //interestJTextField.setText(""); // clear interestJTextField
            //termJTextField.setText(""); // clear termJTextField

        }
        catch (NumberFormatException ex){}
    }





    // method called when user clicks quitJButton
    private void quitJButtonActionPerformed(ActionEvent event)
    {
        try
        {
            System.exit(0);

        }
        catch (NumberFormatException ex){}
    }



    // main method
    public static void main(String[]args)
    {
        Mortgage application = new Mortgage();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.setSize(400, 400);
        application.setVisible(true);
    } // end method main

} // end class Mortgage
0
ghost8067
Asked:
ghost8067
1 Solution
 
hoomanvCommented:
you forgot to add action listener to tableJButton
mistakenly you have added ActionListener to calculateJButton two times
0
 
ghost8067Author Commented:
Thanks, I was looking at it for hour and didn't see it. I need some formatting work, but it is working now.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now