Mortgage calculator with GUI


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Mortgage calculator with GUI

Hybrid View

  1. #1
    Join Date
    Feb 2008
    Posts
    0

    Wink Mortgage calculator with GUI

    The calculator compiles and calculates the monthly payment but the radio buttons are not displaying the information in the fields. I also would like to show the the loan balance and the interest paid for each payment over the term of the loan and allow the user to clear the fields to enter the new information but the clear button doesn't work either. Can anyone help me please? Thanks!
    Code:
    /*
     * Program: Mortgage Calculator with Graphical User Interface
     * Programs accepts user input in 3 text fields: Mortgage Amount, Mortgage Term, and Interest Rate.
     * User can calculate the monthly payment amount from a selection of the following:
     *              7 year loan at 5.35%
     *		15 year loan at 5.50%	 
     *		30 year loan at 5.75%	
     * The user can enter the new available chice for te loan at the top of calulator by getting the 		
     * cursor on the desired field and backspace and enter new data. the user can quit by clicking on EXIT
     */
    	
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    import java.text.*;
    import java.util.*;
    import java.lang.*;
    import javax.swing.border.*;
    import javax.swing.JOptionPane;
    
    // Create MortgageCalculator class
    public class MortgageCalc3 extends JFrame implements ActionListener
    {
    
    // Radio Button Selection
    JPanel rowOne = new JPanel();
    JRadioButton rbOne = new JRadioButton("7 Years/5.35%",true);
    JRadioButton rbTwo = new JRadioButton("15 Years/5.5%",true);
    JRadioButton rbThree = new JRadioButton("30 Years/5.75%",true);
    
    // Text Amount for Mortgage
    JPanel rowTwo = new JPanel();
    JLabel mortgageLabel = new JLabel("Mortgage Amount $", JLabel.LEFT);
    JTextField mortgageText = new JTextField(10);
    JLabel termLabel = new JLabel("Mortgage Term (Years)", JLabel.LEFT);
    JTextField termText = new JTextField(3);
    JLabel intRateLabel = new JLabel("Interest Rate (%)", JLabel.LEFT);
    JTextField intRateText = new JTextField(5);
    JLabel mPaymentLabel = new JLabel("Monthly Payment $", JLabel.LEFT);
    JTextField mPaymentText = new JTextField(10);
    
    // Create buttons to calculate payment, amortizaton, and clear fields
    JPanel rowThree = new JPanel();
    JButton calculateButton = new JButton("CALCULATE");
    JButton clearButton = new JButton("CLEAR");
    JButton exitButton = new JButton("EXIT");
    
    // Create Label for Text Box
    JPanel rowFour = new JPanel();
    JLabel paymentLabel = new JLabel ("Payment #");
    JLabel balLabel = new JLabel (" Balance");
    JLabel ytdPrincLabel = new JLabel (" Principal");
    JLabel ytdIntLabel = new JLabel (" Interest");
    
    // Display area
    JPanel rowFive = new JPanel(new FlowLayout());
    JTextArea textArea = new JTextArea(10, 31);
    JScrollPane scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    
    public MortgageCalc3(){
    super("KARIN'S CALC");
    setSize(400, 485);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container pane = getContentPane();
    JScrollPane scroll = new JScrollPane (textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    FlowLayout layout = new FlowLayout (FlowLayout.LEFT);
    pane.setLayout(layout);
    
    // Menu Selection for Term & Rate
    JMenuItem mOne = new JMenuItem ("7 Years/5.35%");
    JMenuItem mTwo = new JMenuItem ("15 Years/5.5%");
    JMenuItem mThree = new JMenuItem ("30 Years/5.75%");
    JMenu menu = new JMenu ("SELECT TERM & RATE");
    menu.add(mOne);
    menu.addSeparator();
    menu.add(mTwo);
    menu.addSeparator();
    menu.add(mThree);
    
    JMenuBar mBar = new JMenuBar();
    mBar.add(menu);
    setJMenuBar(mBar);
    
    // Listeners
    mOne.addActionListener(this);
    mTwo.addActionListener(this);
    mThree.addActionListener(this);
    
    // Add to the screen
    FlowLayout layout1 = new FlowLayout();
    rowOne.setLayout(layout1);
    
    // Add Radio Button Group
    ButtonGroup btnGrp = new ButtonGroup();
    btnGrp.add(rbOne);
    btnGrp.add(rbTwo);
    btnGrp.add(rbThree);
    rowOne.add(rbOne);
    rowOne.add(rbTwo);
    rowOne.add(rbThree);
    pane.add(rowOne);
    
    // Create a border around the radio buttons
    Border titledRadioBorder =
    BorderFactory.createTitledBorder("Make your selections");
    rowOne.setBorder(titledRadioBorder);
    
    // Listeners
    rbOne.addActionListener(this);
    rbTwo.addActionListener(this);
    rbThree.addActionListener(this);
    
    // Add Labels & Text Boxes
    GridLayout layout2 = new GridLayout (4, 1);
    rowTwo.setLayout(layout2);
    rowTwo.add(mortgageLabel);
    rowTwo.add(mortgageText);
    mortgageText.setText("200000");
    rowTwo.add(termLabel);
    rowTwo.add(termText);
    termText.setText("30");
    rowTwo.add(intRateLabel);
    rowTwo.add(intRateText);
    intRateText.setText("5.75");
    rowTwo.add(mPaymentLabel);
    rowTwo.add(mPaymentText);
    mPaymentText.setEnabled(false);
    pane.add(rowTwo);
    
    // Add Buttons
    FlowLayout layout3 = new FlowLayout (FlowLayout.CENTER, 10, 10);
    rowThree.setLayout (layout3);
    rowThree.add(calculateButton);
    calculateButton.setBackground(Color.green);
    rowThree.add(clearButton);
    clearButton.setBackground(Color.white);
    rowThree.add(exitButton);
    exitButton.setBackground(Color.red);
    pane.add(rowThree);
    
    // Listeners
    calculateButton.addActionListener(this);
    clearButton.addActionListener(this);
    exitButton.addActionListener(this);
    
    // Add Text Box Label
    FlowLayout layout4 = new FlowLayout (FlowLayout.LEFT, 10, 10);
    rowFour.setLayout (layout4);
    rowFour.add(paymentLabel);
    rowFour.add(balLabel);
    rowFour.add(ytdPrincLabel);
    rowFour.add(ytdIntLabel);
    pane.add(rowFour);
    
    // Add Text Box
    FlowLayout layout5 = new FlowLayout (FlowLayout.CENTER, 10, 10);
    rowFive.setLayout(layout5);
    rowFive.add(scroll);
    pane.add(rowFive);
    
    setContentPane(pane);
    setVisible(true);
    }
    
    public static void main(String[] args)throws ParseException {
            DecimalFormat dollarAmount = new DecimalFormat("#,###.00");
            DecimalFormat percentAmount = new DecimalFormat("##.##");
    
     MortgageCalc3 frame = new MortgageCalc3();
    }
    
    public void setResultValue()
     {
       double amount = Double.parseDouble(mortgageText.getText());
       double term = Double.parseDouble(termText.getText());
       double rate = Double.parseDouble(intRateText.getText()) / 100.;
       double result = (amount * ( rate/12))/(1-( Math.pow (1/( 1 +(rate/12)), (term*12))));
    
     
    
       mPaymentText.setText(Double.toString(result));
    }         
    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == calculateButton) {
            setResultValue();}
        
        if (event.getSource() == clearButton) {
            setResultValue();}
    
    
    Object command = event.getSource();
    
    if (command == exitButton){
    System.exit(0);
    }
    }
    }

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    1. Do the buttons display anything at all?
    2. What happens when you hit clear?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

Similar Threads

  1. Code Help with GUI Mortgage Calculator
    By drewdat7001 in forum Java
    Replies: 4
    Last Post: 12-20-2006, 01:55 AM
  2. Help With GUI Mortgage Calculator
    By drewdat7001 in forum Java
    Replies: 1
    Last Post: 10-30-2006, 12:34 AM
  3. Replies: 7
    Last Post: 03-07-2006, 05:34 PM
  4. Gui Freeze after re-connect
    By lstwil in forum Java
    Replies: 0
    Last Post: 05-29-2005, 09:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center