I'm trying to figure out how to add a reset button and a calculation button to my program. I'm lost here. The program works when the user put in the loan amount, lenght of loan and interest rate. I need a button to reset the fields so another user can use these fields and also I need a calculation botton to calculate all the user input instead of just hitting the return key, here is my program.

//This program shows the payment of a mortgage after the amount,interest rate and length of
//loan have been entered in the fields*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.text.*;

public class SchoolsMortgage extends JPanel implements PropertyChangeListener {
    //declaration of label,string and fields
     double amount = 0;
     double rate = 0;
     int years = 0;
     JLabel amountLabel;
     JLabel rateLabel;
     JLabel yearsLabel;
     JLabel paymentLabel;
     JLabel calculation;
     JLabel  reset;

    static String amountString = "Balance Amount ";
    static String rateString = "APR  ";
    static String yearsString = "Years ";
    static String paymentString = "Monthly Payment ";

    JFormattedTextField rateField;
    JFormattedTextField yearsField;
    JFormattedTextField paymentField;
    JFormattedTextField amountField; 

    NumberFormat amountFormat;
    NumberFormat percentFormat;
    NumberFormat paymentFormat;



    public SchoolsMortgage() {
        super(new BorderLayout());
        double payment = findingpayment(amount,rate,years);
        setUpFormats();
        amountLabel = new JLabel(amountString);
        rateLabel = new JLabel(rateString);
        yearsLabel = new JLabel(yearsString);
        paymentLabel = new JLabel(paymentString);



        //creating text fields for amount,rate,year and apr
        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);
        yearsField = new JFormattedTextField();
        yearsField.setValue(new Integer(years));
        yearsField.setColumns(10);
        yearsField.addPropertyChangeListener("value", this);
        paymentField = new JFormattedTextField(paymentFormat);
        paymentField.setValue(new Double(payment));
        paymentField.setColumns(10);
        paymentField.addPropertyChangeListener("value", this);
        //Tell accessibility tools about label/textfield pairs.
        amountLabel.setLabelFor(amountField);
        rateLabel.setLabelFor(rateField);
        yearsLabel.setLabelFor(yearsField);
        paymentLabel.setLabelFor(paymentField);
        //left and right location of JPanel
        JPanel labelPane = new JPanel(new GridLayout(0,1));
        labelPane.add(amountLabel);
        labelPane.add(rateLabel);
        labelPane.add(yearsLabel);
        labelPane.add(paymentLabel);

        //Layout the text fields in a panel.
        JPanel fieldPane = new JPanel(new GridLayout(0,1));
        fieldPane.add(amountField);
        fieldPane.add(rateField);
        fieldPane.add(yearsField);
        fieldPane.add(paymentField);



        //Put the panels in this panel, labels on left,
        //text fields on right.
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        add(labelPane, BorderLayout.CENTER);
        add(fieldPane, BorderLayout.LINE_END);
    }
      //getting information entered in the different fields
    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 == yearsField) {
            years = ((Number)yearsField.getValue()).intValue();
        }
        double payment = findingpayment(amount, rate, years);
        paymentField.setValue(new Double(payment));
    }
        private static void createAndShowGUI() {
        //Create and set up the  title window.
        JFrame frame = new JFrame("Schools Mortgage Formula");

        //Create and set up the content pane.
        JComponent newContentPane = new PaulsMortgage();
        newContentPane.setOpaque(true); 
        frame.setContentPane(newContentPane);
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
    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() {
                createAndShowGUI();
            }
        });
    }
    //Compute the monthly payment based on the loan amount,
    //APR, and length of loan.
    double findingpayment (double loanAmt, double rate, int years) {
        double I, partial1, bottom, answer;
        years *= 12;        
        if (rate > 0.01) {
            I = rate / 100.0 / 12.0;         
            partial1 = Math.pow((1 + I), (0.0 - years));
            bottom = (1 - partial1) / I;
        } else { //rate ~= 0
            bottom = years;
        }
        answer = (-1 * loanAmt) / bottom;
        return answer;
    }
    //Making sure that numbering is in the right format 2 and 3 digit after decimal
    private void setUpFormats() {
        amountFormat = NumberFormat.getNumberInstance();
        percentFormat = NumberFormat.getNumberInstance();
        percentFormat.setMinimumFractionDigits(3);
        paymentFormat = NumberFormat.getCurrencyInstance();
    }
}

Recommended Answers

All 2 Replies

Member Avatar for iamthwee

Please use code tags.

What you need is ActionListener, addActionListener, and actionPerformed,
all used almost exactly like the equivalent methods in the ProperChangeListener you are using now. Check out the API doc for JButton and ActionListener, and maybe the Swing Tutorial (almost all of the Swing tutorial examples use an ActionListener).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.