Results 1 to 2 of 2
  1. #1
    smallmos1 is offline Member
    Join Date
    Sep 2010
    Rep Power

    Default problem displaying calculation in JText field

    this program is supposed to calculate and display monthly mortgage repayments and the total amount to be repaid. i'm only on the display monthly repayment part at the moment, and can't seem to get my figure to appear in the text field.

    all help welcome.

    Java Code:
    package worksheet1;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    public class CalculateMortgage extends JFrame implements ActionListener
    	private JTextField rate, year, loan, monthlyPay, totalPay;
    	private JButton calculate, clear;
    	private JPanel p1, p2;
    	Container c;
    	public CalculateMortgage()
    		//set window size, title and close operation
    		setSize(400, 200);
    		setTitle("Mortgage Application");
    		//initialise text fields and buttons
    		rate = new JTextField(10);
    		year = new JTextField(10);
    		loan = new JTextField(10);
    		monthlyPay = new JTextField(10);
    		totalPay = new JTextField(10);
    		calculate = new JButton("Compute Mortgage");
    		//initialise first panel
    		p1 = new JPanel();
    		//set grid layout with two columns and five rows
    		p1.setLayout(new GridLayout(5,2));
    			p1.add(new Label("Interest Rate"));
    			p1.add(new Label("Years"));
    			p1.add(new Label("Loan Amount"));
    			p1.add(new Label("Monthly Payment"));
    			p1.add(new Label("Total Payment"));
    		//initialise second panel
    		p2 = new JPanel();
    		//set flow layout to right
    		p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
    		//place panels inside container and specify location
    		add(p1, BorderLayout.CENTER);
    		add(p2, BorderLayout.SOUTH);
    		//register action listener
    	//method to calculate pay
    	public void actionPerformed(ActionEvent e)
    		//if the button clicked is calculate
    			//these variables = the value entered in the text fields
    			double interest = (Double.valueOf(rate.getText())).doubleValue();
    			int years = (Integer.valueOf(year.getText())).intValue();
    			double amount = (Double.valueOf(rate.getText())).doubleValue();
    			//calculate monthly repayments
    			double interestRate = interest / 10; 
    			double totalAmount = years * amount;
    			double totalMonths = years * 12;
    			double monthlyRepayment = totalAmount * years * interestRate / totalMonths;
    	public static void main(String args[])
    		CalculateMortgage one = new CalculateMortgage();

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Rep Power


    Take a close look at your actionListener implementation, in particular how you are evaluating the action command. Add some println values to see if the if statement in the listener is actually being evaluated to true under the conditions you think it should

Similar Threads

  1. Simple calculation method problem.
    By carman12 in forum New To Java
    Replies: 11
    Last Post: 12-29-2010, 01:56 AM
  2. Problem with calculation
    By ibrafiqb in forum New To Java
    Replies: 3
    Last Post: 11-24-2010, 05:02 PM
  3. Two problem for my rssi calculation program
    By iamchoilan in forum Java 2D
    Replies: 12
    Last Post: 04-27-2008, 07:56 PM
  4. Two problem for my rssi calculation program
    By iamchoilan in forum Advanced Java
    Replies: 3
    Last Post: 04-25-2008, 03:49 PM
  5. Problem with Calculation ....
    By danny000 in forum New To Java
    Replies: 1
    Last Post: 04-15-2008, 02:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts