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

    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);
    		setResizable(false);
    		setTitle("Mortgage Application");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		
    		//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(rate);
    			p1.add(new Label("Years"));
    			p1.add(year);
    			p1.add(new Label("Loan Amount"));
    			p1.add(loan);
    			p1.add(new Label("Monthly Payment"));
    			p1.add(monthlyPay);
    			p1.add(new Label("Total Payment"));
    			p1.add(totalPay);
    		
    		//initialise second panel
    		p2 = new JPanel();
    		//set flow layout to right
    		p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
    			p2.add(calculate);
    			
    		//place panels inside container and specify location
    		add(p1, BorderLayout.CENTER);
    		add(p2, BorderLayout.SOUTH);
    		
    		//register action listener
    		calculate.addActionListener(this);
    	}
    	
    	//method to calculate pay
    	public void actionPerformed(ActionEvent e)
    	{
    		//if the button clicked is calculate
    		if(e.getActionCommand().equals(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;
    			monthlyPay.setText(String.valueOf(monthlyRepayment));
    		}
    	}
    	
    	public static void main(String args[])
    	{
    		CalculateMortgage one = new CalculateMortgage();
    		one.setVisible(true);
    	}
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    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
  •