jjbraxton
asked on
Mortgage calculator with GUI- Clear button not working
This is a mortgage calculator with GUI. The clear button is supposed top clear all fields so that the user can start over. Please help, I get errors when I click on the clear button.
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
{
private static String default_loan = null;
private static String default_term = null;
private static String default_rate = null;
private static String default_payment = null;
//setup format for currency
DecimalFormat money = new DecimalFormat("$###,###.00 ");
//Declare output stream
DataOutputStream output;
//Construct a panel for each row
private JPanel firstRow = new JPanel();
private JPanel secondRow = new JPanel();
private JPanel thirdRow = new JPanel();
private JPanel fourthRow = new JPanel();
//Construct a panel for the fields and buttons
JPanel fieldPanel = new JPanel();
JPanel buttonPanel = new JPanel();
private JTextField loanField;
private JTextField termField;
private JTextField rateField;
private JTextField paymentField;
//Construct labels and text boxes
private JLabel loanLabel = new JLabel("Mortgage Amount: ");
private JTextField p = new JTextField(15);
private JLabel termLabel = new JLabel("Term (in years):");
private JTextField t = new JTextField(5);
private JLabel rateLabel = new JLabel("Interest Rate:");
private JTextField r = new JTextField(5);
private JLabel paymentLabel = new JLabel("Payment:");
private JTextField m = new JTextField(15);
private JLabel displayPayment = new JLabel();
//Construct Buttons
private JButton calculateButton = new JButton("Calculate Payment");
private JButton clearButton = new JButton("Clear");
private JButton quitButton = new JButton ("Quit");
public static void main(String[] args)
{
//Set look and feel for the interface
try
{
UIManager.setLookAndFeel(" com.sun.ja va.swing.p laf.motif. MotifLookA ndFeel");
}
catch (Exception e)
{
}
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(rowSetu p);
secondRow.setLayout(rowSet up);
thirdRow.setLayout(rowSetu p);
fourthRow.setLayout(rowSet up);
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENT ER));
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGH T));
//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(m);
fourthRow.add(displayPayme nt);
//Add rows to panel
fieldPanel.add(firstRow);
fieldPanel.add(secondRow);
fieldPanel.add(thirdRow);
fieldPanel.add(fourthRow);
//add button to panel
buttonPanel.add(calculateB utton);
buttonPanel.add(clearButto n);
buttonPanel.add(quitButton );
//Add panels to frame
c.add(fieldPanel, BorderLayout.CENTER);
c.add(buttonPanel, BorderLayout.SOUTH);
//add fuctionality to the button
calculateButton.addActionL istener(th is);
clearButton.addActionListe ner(this);
quitButton.addActionListen er(this);
}
public void actionPerformed(ActionEven t e)
{
Object source = e.getSource();
if(source == clearButton)
{
loanField.setText(null);
termField.setText(null);
rateField.setText(null);
paymentField.setText(null) ;
}
if(source == quitButton)
{
System.exit(0);
}
//This is going to call the method for doing the math
mortCal();
}
public void mortCal()
{
double principle = Double.parseDouble(p.getTe xt());
double rate = Double.parseDouble(r.getTe xt());
double term = Double.parseDouble(t.getTe xt());
double interest = (rate/100/12);
double payment = (principle*interest)/(1-Ma th.pow(1/( 1+interest ), term*12));
String displayPayment = (money.format(payment));
paymentLabel.setText("" + "$" + payment);
}
}
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
{
private static String default_loan = null;
private static String default_term = null;
private static String default_rate = null;
private static String default_payment = null;
//setup format for currency
DecimalFormat money = new DecimalFormat("$###,###.00
//Declare output stream
DataOutputStream output;
//Construct a panel for each row
private JPanel firstRow = new JPanel();
private JPanel secondRow = new JPanel();
private JPanel thirdRow = new JPanel();
private JPanel fourthRow = new JPanel();
//Construct a panel for the fields and buttons
JPanel fieldPanel = new JPanel();
JPanel buttonPanel = new JPanel();
private JTextField loanField;
private JTextField termField;
private JTextField rateField;
private JTextField paymentField;
//Construct labels and text boxes
private JLabel loanLabel = new JLabel("Mortgage Amount: ");
private JTextField p = new JTextField(15);
private JLabel termLabel = new JLabel("Term (in years):");
private JTextField t = new JTextField(5);
private JLabel rateLabel = new JLabel("Interest Rate:");
private JTextField r = new JTextField(5);
private JLabel paymentLabel = new JLabel("Payment:");
private JTextField m = new JTextField(15);
private JLabel displayPayment = new JLabel();
//Construct Buttons
private JButton calculateButton = new JButton("Calculate Payment");
private JButton clearButton = new JButton("Clear");
private JButton quitButton = new JButton ("Quit");
public static void main(String[] args)
{
//Set look and feel for the interface
try
{
UIManager.setLookAndFeel("
}
catch (Exception e)
{
}
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
firstRow.setLayout(rowSetu
secondRow.setLayout(rowSet
thirdRow.setLayout(rowSetu
fourthRow.setLayout(rowSet
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENT
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGH
//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(m);
fourthRow.add(displayPayme
//Add rows to panel
fieldPanel.add(firstRow);
fieldPanel.add(secondRow);
fieldPanel.add(thirdRow);
fieldPanel.add(fourthRow);
//add button to panel
buttonPanel.add(calculateB
buttonPanel.add(clearButto
buttonPanel.add(quitButton
//Add panels to frame
c.add(fieldPanel, BorderLayout.CENTER);
c.add(buttonPanel, BorderLayout.SOUTH);
//add fuctionality to the button
calculateButton.addActionL
clearButton.addActionListe
quitButton.addActionListen
}
public void actionPerformed(ActionEven
{
Object source = e.getSource();
if(source == clearButton)
{
loanField.setText(null);
termField.setText(null);
rateField.setText(null);
paymentField.setText(null)
}
if(source == quitButton)
{
System.exit(0);
}
//This is going to call the method for doing the math
mortCal();
}
public void mortCal()
{
double principle = Double.parseDouble(p.getTe
double rate = Double.parseDouble(r.getTe
double term = Double.parseDouble(t.getTe
double interest = (rate/100/12);
double payment = (principle*interest)/(1-Ma
String displayPayment = (money.format(payment));
paymentLabel.setText("" + "$" + payment);
}
}
You are not initialzing "loanField" variable.
> loanField.setText(null);
> termField.setText(null);
> rateField.setText(null);
> paymentField.setText(null) ;
try changing this to:
p.setText(null);
t.setText(null);
r.setText(null);
m.setText(null);
> termField.setText(null);
> rateField.setText(null);
> paymentField.setText(null)
try changing this to:
p.setText(null);
t.setText(null);
r.setText(null);
m.setText(null);
the function mortCal() should also be changed as:
public void mortCal() {
if (p.getText().trim().length () == 0 || r.getText().trim().length( ) == 0 || t.getText().trim().length( ) == 0) {
paymentLabel.setText("Paym ent:");
return;
}
double principle = Double.parseDouble(p.getTe xt());
double rate = Double.parseDouble(r.getTe xt());
double term = Double.parseDouble(t.getTe xt());
double interest = (rate / 100 / 12);
double payment = (principle * interest) / (1 - Math.pow(1 / (1 + interest), term * 12));
displayPayment.setText("" + NumberFormat.getCurrencyIn stance(Loc ale.US).fo rmat(payme nt));
}
public void mortCal() {
if (p.getText().trim().length
paymentLabel.setText("Paym
return;
}
double principle = Double.parseDouble(p.getTe
double rate = Double.parseDouble(r.getTe
double term = Double.parseDouble(t.getTe
double interest = (rate / 100 / 12);
double payment = (principle * interest) / (1 - Math.pow(1 / (1 + interest), term * 12));
displayPayment.setText("" + NumberFormat.getCurrencyIn
}
In my last comment,
> paymentLabel.setText("Paym ent:");
should be:
displayPayment.setText(nul l);
> paymentLabel.setText("Paym
should be:
displayPayment.setText(nul
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>The mortcal() need no te changed
mortcalc() need not be changed
mortcalc() need not be changed
ASKER
Thanks. It compiled without errors, but now the Calculate button is not working. This the program with your suggested changes.
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
{
private static String default_loan = null;
private static String default_term = null;
private static String default_rate = null;
private static String default_payment = null;
//setup format for currency
DecimalFormat money = new DecimalFormat("$###,###.00 ");
//Declare output stream
DataOutputStream output;
//Construct a panel for each row
private JPanel firstRow = new JPanel();
private JPanel secondRow = new JPanel();
private JPanel thirdRow = new JPanel();
private JPanel fourthRow = new JPanel();
//Construct a panel for the fields and buttons
JPanel fieldPanel = new JPanel();
JPanel buttonPanel = new JPanel();
private JTextField loanField;
private JTextField termField;
private JTextField rateField;
private JTextField paymentField;
//Construct labels and text boxes
private JLabel loanLabel = new JLabel("Mortgage Amount: ");
private JTextField p = new JTextField(15);
private JLabel termLabel = new JLabel("Term (in years):");
private JTextField t = new JTextField(5);
private JLabel rateLabel = new JLabel("Interest Rate:");
private JTextField r = new JTextField(5);
private JLabel paymentLabel = new JLabel("Payment:");
private JTextField m = new JTextField(15);
private JLabel displayPayment = new JLabel();
//Construct Buttons
private JButton calculateButton = new JButton("Calculate Payment");
private JButton clearButton = new JButton("Clear");
private JButton quitButton = new JButton ("Quit");
public static void main(String[] args)
{
//Set look and feel for the interface
try
{
UIManager.setLookAndFeel(" com.sun.ja va.swing.p laf.motif. MotifLookA ndFeel");
}
catch (Exception e)
{
}
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(rowSetu p);
secondRow.setLayout(rowSet up);
thirdRow.setLayout(rowSetu p);
fourthRow.setLayout(rowSet up);
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENT ER));
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGH T));
//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(m);
fourthRow.add(displayPayme nt);
//Add rows to panel
fieldPanel.add(firstRow);
fieldPanel.add(secondRow);
fieldPanel.add(thirdRow);
fieldPanel.add(fourthRow);
//add button to panel
buttonPanel.add(calculateB utton);
buttonPanel.add(clearButto n);
buttonPanel.add(quitButton );
//Add panels to frame
c.add(fieldPanel, BorderLayout.CENTER);
c.add(buttonPanel, BorderLayout.SOUTH);
//add fuctionality to the button
calculateButton.addActionL istener(th is);
clearButton.addActionListe ner(this);
quitButton.addActionListen er(this);
}
public void actionPerformed(ActionEven t e)
{
Object source = e.getSource();
if(source == clearButton)
{
p.setText(null);
t.setText(null);
r.setText(null);
m.setText(null);
}
if(source == quitButton)
{
System.exit(0);
}
//This is going to call the method for doing the math
mortCal();
}
public void mortCal()
{
{
if(p.getText().trim().leng th()==0||r .getText() .trim().le ngth()==0| |t.getText ().trim(). length()== 0)
{
displayPayment.setText(nul l);
return;
}
double principle = Double.parseDouble(p.getTe xt());
double rate = Double.parseDouble(r.getTe xt());
double term = Double.parseDouble(t.getTe xt());
double interest = (rate/100/12);
double payment = (principle*interest)/(1-Ma th.pow(1/( 1+interest ), term*12));
displayPayment.setText("" + NumberFormat.getCurrencyIn stance().f ormat(paym ent));
displayPayment.setText(nul l);
}
}
}
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
{
private static String default_loan = null;
private static String default_term = null;
private static String default_rate = null;
private static String default_payment = null;
//setup format for currency
DecimalFormat money = new DecimalFormat("$###,###.00
//Declare output stream
DataOutputStream output;
//Construct a panel for each row
private JPanel firstRow = new JPanel();
private JPanel secondRow = new JPanel();
private JPanel thirdRow = new JPanel();
private JPanel fourthRow = new JPanel();
//Construct a panel for the fields and buttons
JPanel fieldPanel = new JPanel();
JPanel buttonPanel = new JPanel();
private JTextField loanField;
private JTextField termField;
private JTextField rateField;
private JTextField paymentField;
//Construct labels and text boxes
private JLabel loanLabel = new JLabel("Mortgage Amount: ");
private JTextField p = new JTextField(15);
private JLabel termLabel = new JLabel("Term (in years):");
private JTextField t = new JTextField(5);
private JLabel rateLabel = new JLabel("Interest Rate:");
private JTextField r = new JTextField(5);
private JLabel paymentLabel = new JLabel("Payment:");
private JTextField m = new JTextField(15);
private JLabel displayPayment = new JLabel();
//Construct Buttons
private JButton calculateButton = new JButton("Calculate Payment");
private JButton clearButton = new JButton("Clear");
private JButton quitButton = new JButton ("Quit");
public static void main(String[] args)
{
//Set look and feel for the interface
try
{
UIManager.setLookAndFeel("
}
catch (Exception e)
{
}
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
firstRow.setLayout(rowSetu
secondRow.setLayout(rowSet
thirdRow.setLayout(rowSetu
fourthRow.setLayout(rowSet
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENT
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGH
//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(m);
fourthRow.add(displayPayme
//Add rows to panel
fieldPanel.add(firstRow);
fieldPanel.add(secondRow);
fieldPanel.add(thirdRow);
fieldPanel.add(fourthRow);
//add button to panel
buttonPanel.add(calculateB
buttonPanel.add(clearButto
buttonPanel.add(quitButton
//Add panels to frame
c.add(fieldPanel, BorderLayout.CENTER);
c.add(buttonPanel, BorderLayout.SOUTH);
//add fuctionality to the button
calculateButton.addActionL
clearButton.addActionListe
quitButton.addActionListen
}
public void actionPerformed(ActionEven
{
Object source = e.getSource();
if(source == clearButton)
{
p.setText(null);
t.setText(null);
r.setText(null);
m.setText(null);
}
if(source == quitButton)
{
System.exit(0);
}
//This is going to call the method for doing the math
mortCal();
}
public void mortCal()
{
{
if(p.getText().trim().leng
{
displayPayment.setText(nul
return;
}
double principle = Double.parseDouble(p.getTe
double rate = Double.parseDouble(r.getTe
double term = Double.parseDouble(t.getTe
double interest = (rate/100/12);
double payment = (principle*interest)/(1-Ma
displayPayment.setText("" + NumberFormat.getCurrencyIn
displayPayment.setText(nul
}
}
}
have you tried mine??
its working
its working
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.