[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

07/15/2007 at 05:57PM PDT, ID: 22697782
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.0

GUI  Problem in Java Program

Asked by rhivka in New to Java Programming

Tags: java, mortgage, calculator, gui, program

Hello experts! I'm working on this good 'ole mortgage calculator program, and trust me...I've looked for solutions and I've stared at my code for days and compared, but I cannot get the radio button row to appear below the principle input box instead of across the top. Simple, I'm sure. But I'm stumpled. Now I know this program has other issues/problems going on before it does what it's meant to do, but for now I want to get the GUI set up. Could you please take a look and see what I'm doing wrong? Thank you in advance!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

public class MortgageCalculator extends JFrame implements ActionListener
{
    double rate = 0;  //APR
    double monthlyPayment = 0; //Monthly payment amount
    double principle = 0; //Amount of loan
    int term = 0; //Mortgage term in years
    double interest = 0; //Interest, in months
    int notePeriod = 0; //Mortgage term in months

    String mTerm[] = {"7", "15", "30"};
    String mRate[] = {"5.35%", "5.50%", "5.75%"};

    //Labels and Input Fields

    JPanel row1 = new JPanel();
    JLabel mortgageLabel = new JLabel("MORTGAGE CALCULATOR", JLabel.CENTER);

    JPanel row2 = new JPanel(new GridLayout(1, 2));
    JLabel principleLabel = new JLabel("Mortgage Principle $",JLabel.LEFT);
    JTextField principleTxt = new JTextField(8);

    JPanel radioPanel = new JPanel();
    JRadioButton buttonA = new JRadioButton("7 Years at 5.35%" , false);
    JRadioButton buttonB = new JRadioButton("15 Years at 5.50%" , false);
    JRadioButton buttonC = new JRadioButton("30 Years at 5.75%", false);

    JPanel row5 = new JPanel(new GridLayout(1, 2));
    JLabel monthlyPaymentLabel = new JLabel("Monthly Payment $", JLabel.LEFT);
    JTextField monthlyPaymentTxt = new JTextField(8);

    //Create buttons
    JPanel button = new JPanel(new FlowLayout(FlowLayout.CENTER, 8, 8));
    JButton amortizeButton = new JButton("Amortize Payments");
    JButton clearButton = new JButton("Clear");
    JButton exitButton = new JButton("Exit");
    JButton calculateButton = new JButton("Calculate");

    JTextArea displayArea = new JTextArea(10, 30);
    JScrollPane scroll = new JScrollPane(displayArea);

    public MortgageCalculator()
    {
        super ("Mortgage Payment Calculator"); //Header
        setSize(450, 400); //GUI frame size
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Container pane = getContentPane();

        Border rowborder = new EmptyBorder( 3, 10, 3, 10 ); //Border around elements inside frame

        //Pane and row properties, GUI layout
        JPanel panel = new JPanel(new GridLayout(4,1));
        panel.add(row1);
        row1.add(mortgageLabel);
        row1.setMaximumSize( new Dimension( 1000, row1.getMinimumSize().height));
        row1.setBorder( rowborder);

        panel.add(row2);
        row2.add(principleLabel);
        row2.add(principleTxt);
        row2.setMaximumSize( new Dimension( 1000, row2.getMinimumSize().height));
        row2.setBorder( rowborder);

        ButtonGroup bgroup = new ButtonGroup();
        bgroup.add(buttonA);
        bgroup.add(buttonB);
        bgroup.add(buttonC);

        radioPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 4, 4 ));
        radioPanel.add(buttonA);
        radioPanel.add(buttonB);
        radioPanel.add(buttonC);

        pane.add(radioPanel);
        radioPanel.setMaximumSize( new Dimension( 10000, radioPanel.getMinimumSize().height));
        radioPanel.setBorder( rowborder);

        panel.add(row5);
        row5.add(monthlyPaymentLabel);
        row5.add(monthlyPaymentTxt);
        monthlyPaymentTxt.setEnabled(false); //Set payment amount uneditable
        row5.setMaximumSize( new Dimension( 1000, row5.getMinimumSize().height));
        row5.setBorder( rowborder);

        panel.add(button); //Add buttons
        button.add(calculateButton);
        button.add(clearButton);
        button.add(exitButton);
        button.add(amortizeButton);
        pane.add(panel);
        button.setMaximumSize( new Dimension( 1000, button.getMinimumSize().height));

       scroll.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        pane.add(scroll);

        pane.setLayout(new BoxLayout( pane, BoxLayout.Y_AXIS));
        setVisible(true);
        setContentPane(pane);

        amortizeButton.addActionListener(this); //Add listeners
        clearButton.addActionListener(this);
        exitButton.addActionListener(this);
        calculateButton.addActionListener(this);
        buttonA.addActionListener(this);
        buttonB.addActionListener(this);
        buttonC.addActionListener(this);

    }

    public void actionPerformed(ActionEvent event)
    {
        Object command = event.getSource();

           if(command == calculateButton)
      {
          try
          {
              principle = Double.parseDouble(principleTxt.getText());
          }
      catch(NumberFormatException e)
      {
          //Select rate and term
          if(buttonA.isSelected() == true)
          {
              rate = 5.35;
              term = 7;
          }
          else if(buttonB.isSelected() == true)
          {
              rate = 5.5;
              term = 15;
          }
          else if (buttonC.isSelected() == true)
          {
              rate = 5.75;
              term = 30;
          }
          else //none of the buttons  are selected,  this is an actual error.  Throw an exception
          {
              JOptionPane.showMessageDialog(null, "Invaild Entry!  Please Try Again", "ERROR", JOptionPane.ERROR_MESSAGE);
          }
                     }

      //Calculate monthly interest rate
      interest = ((rate / 100) / 12);

      //Calculate total number of payments
      notePeriod = term * 12; // (for example, 5 years means notePeriod = 60)

       //Calculate monthly payment
      if (rate == 0.0)
      {
          monthlyPayment = principle / notePeriod;
      }
      else
          {
              monthlyPayment = (principle * interest) / (1 - Math.pow(1 + interest, -notePeriod));
          }

      //Currency formatter
      NumberFormat myCurrencyFormatter;
      myCurrencyFormatter = NumberFormat.getCurrencyInstance(Locale.US);
      monthlyPaymentTxt.setText(myCurrencyFormatter.format(monthlyPayment));
        }

        if(command == clearButton)
        {
             principleTxt.setText(null);
             monthlyPaymentTxt.setText(null);
             displayArea.setText(null);
        }

        if(command == exitButton)
        {
             System.exit(0);
        }
        if (command == amortizeButton)
        {
      double year = 0;
      double interest = 0;
      double balance = 0;
      double monthlyInterest = 0;
      int x = 1;
      int months = term * 12;
      String titles = "Month\t Principle\tInterest\tBalance\n";

      interest = (rate / 100) /12;
      balance = principle;

      displayArea.setText(titles);

      for (x = 1; x <= months; ++x)
      {
          //Calculate Detailed Payment Info
          monthlyInterest = balance * interest;

          if(interest != 0)

          year = monthlyPayment - monthlyInterest;
          balance = principle - year;

          //Format Variables
          DecimalFormat df = new DecimalFormat("\u00A4#,##0.00"); //Crrency
          DecimalFormat pf = new DecimalFormat("#,##0.00%"); //Percentages
          DecimalFormat mi = new DecimalFormat("#,##0.000%"); //Percentages

          //Positions the Cursor to the Top of the TextArea
          displayArea.setCaretPosition(0);
          displayArea.append((months +1) + ")\t"+df.format(principle)+"\t"+df.format(interest)+"\t"+df.format(balance)+"\n");
      }
            }
    }

    public void Payment()
    {
    //Get user input
    principle = Double.parseDouble(principleTxt.getText());

    //Calculate monthly interest rate
    interest = ((rate / 100) / 12);

    //Calculate total number of payments
    notePeriod = term * 12;

    //Calculate monthly payment
    if (rate == 0.0)
    {
        monthlyPayment = principle / notePeriod;
    }
    else
        {
            monthlyPayment = (principle * interest) / (1 - Math.pow(1 + interest, -notePeriod));
        }

    //Currency formatter
    NumberFormat myCurrencyFormatter;
    myCurrencyFormatter = NumberFormat.getCurrencyInstance(Locale.US);
    monthlyPaymentTxt.setText(myCurrencyFormatter.format(monthlyPayment));
}
    public static void main (String[] arguments) //Main Method
    {
        MortgageCalculator smc = new MortgageCalculator();
        smc.setVisible(true);
        smc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
 } //End of program
[+][-]07/15/07 06:38 PM, ID: 19492663

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/15/07 06:38 PM, ID: 19492664

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/15/07 10:46 PM, ID: 19493294

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: New to Java Programming
Tags: java, mortgage, calculator, gui, program
Sign Up Now!
Solution Provided By: ksivananth
Participating Experts: 3
Solution Grade: A
 
 
[+][-]07/16/07 12:03 AM, ID: 19493479

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/16/07 02:25 PM, ID: 19499881

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/16/07 06:34 PM, ID: 19501586

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]07/16/07 09:45 PM, ID: 19502273

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20090824-EE-VQP-74