We help IT Professionals succeed at work.

Problem with ActionEvent and JButton

omoa
omoa asked
on
835 Views
Last Modified: 2008-01-09
I can not figure out what I am missing to make my exit button work in the below program. I am sure it is something simple but I am at a lost as to what it could be. The program complies without error and all my other buttons work correctly.  I am believe the problem revolves around the ActionEvent  for my  exit button but can't find it. What am I doing wrong?


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

class mortCalcWk3 extends JFrame implements ActionListener {

//Establish Container for User Input
JTextField principalText = new JTextField(15);
JTextField yearIntrateText = new JTextField(15);
JTextField numYearsText = new JTextField(15);
JTextField monthlyPaymentsText = new JTextField(15);
JLabel validateLabel = new JLabel();


mortCalcWk3() {
super("McBride Financial Services Mortgage Calculator");
setSize(390, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
FlowLayout flow = new FlowLayout(FlowLayout.RIGHT);
pane.setLayout(flow);

JPanel row1 = new JPanel();
JLabel principalLabel = new JLabel("Loan Amount:$");
row1.add(principalLabel);
row1.add(principalText);
pane.add(row1);

JPanel row2 = new JPanel();
JLabel yearIntrateLabel = new JLabel("Yearly Interest Rate:");
row2.add(yearIntrateLabel);
row2.add(yearIntrateText);
pane.add(row2);

JPanel row3 = new JPanel();
JLabel numYearsLabel = new JLabel("Term of Loan(Years):");
row3.add(numYearsLabel);
row3.add(numYearsText);
pane.add(row3);

JPanel row4 = new JPanel();
JButton calculate = new JButton("Calculate");
calculate.addActionListener(this);
row4.add(calculate);
pane.add(row4);
JButton reset = new JButton("Reset Calculator");
reset.addActionListener(this);
row4.add(reset);
pane.add(row4);
JButton exit = new JButton("Exit?");
exit.addActionListener(this);
row4.add(exit);
pane.add(row4);

JPanel row5 = new JPanel();
JLabel monthlyPaymentsLabel = new JLabel("Total Monthly Payment:");
row5.add(monthlyPaymentsLabel);
row5.add(monthlyPaymentsText);
pane.add(row5);
setContentPane(pane);
setVisible(true);

}

public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command == "Calculate")
compute();
if (command == "Reset Calculator")
clearAllFields();
if (command == "Exit")
exit();}
void compute()
{
try{
validateLabel.setText("");
validateLabel.validate();


Double yearIntrate = new Double (yearIntrateText.getText());
Double principal = new Double (principalText.getText());
Integer numYears = new Integer (numYearsText.getText());

double payFreq = 12;
double interestRateMonths;
double monthlyPayment;
double numPayments;


interestRateMonths = (yearIntrate.doubleValue() / 100) / payFreq;
numPayments = payFreq * numYears.intValue();

double temp = Math.pow(1 + interestRateMonths, (-1*numPayments));
monthlyPayment = principal.doubleValue() * interestRateMonths / (1 - (temp));

//Format used to set two decimal places.
DecimalFormat currency = new DecimalFormat("$0.00");

monthlyPaymentsText.setText(currency.format(monthlyPayment));

} catch(Exception error)
{
validateLabel.setText("Invalid Entry! Try again.");
}
}

void clearAllFields(){
principalText.setText("");
yearIntrateText.setText("");
numYearsText.setText("");
monthlyPaymentsText.setText("");
}
void exit(){
System.exit(0);

}

public static void main(String[] arguments)
{
mortCalcWk3 mortCalcWk3  = new mortCalcWk3 ();
      }
}// end program
Comment
Watch Question

Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
UrosVidojevic:

You are a GOD SENT.Thank you so much for being my second set of eyes. It is funny how a simple typo can make you think you are loosing your mind.  Weird how I can figure out the hard stuff but the easy things send me looking for the Tylenol.

Thank you again -500 points award.

OMOA

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
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
Empower Your Career
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

Ask ANY Question

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

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.