Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Need Help on Updating a GUI Java

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need Help on Updating a GUI Java

    Hi all,
    I am working on a java program and I need to update it so that it does the following:
    ****Write the program in Java (with a graphical user interface) so that it will allow the user to select which way they want to calculate a mortgage: by input of the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage payment or by input of the amount of a mortgage and then select from a menu of mortgage loans:

    - 7 year at 5.35%
    - 15 year at 5.5 %
    - 30 year at 5.75%

    In either case, display the mortgage payment amount and then, list the loan balance and interest paid for each payment over the term of the loan. Allow the user to loop back and enter a new amount and make a new selection, or quit. Insert comments in the program to document the program.
    ***
    Not exactly sure what I need to do to make it do this.
    Here is my code as of now:
    /* Dwight Welsh
    GUI Java Mortgage Calculation Program
       */
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.NumberFormat;
     
    public class Mortgage extends JFrame
    {
    	// define window width and height
    	private static final int FRAME_WIDTH = 600;
    	private static final int FRAME_HEIGHT = 800;
     
    	// define text field for amount of the mortgage
    	private JTextField amountText;
     
    	// define text area to display mortgage payment amount followed
    	// by the loan balance and interest paid for each payment over
    	// the term of the loan
    	private JTextArea outputTextArea;
     
    	// term (in years) array
    	private int[] terms = {7, 15, 30};
    	// annual interest rate (%) array
    	private double[] rates = {5.35, 5.50, 5.75};
     
    	// constructor
    	public Mortgage()
    	{
    		super("Mortgage Payment Calculator");
     
    		Container container = getContentPane();
     
    		// set the Border Layout
    		container.setLayout(new BorderLayout());
     
            // create label
    		JLabel label = new JLabel("Amount of the mortgage: ");
     
    		// create text field
    		amountText = new JTextField(10);
     
    		// create menu
    		JMenuBar menuBar = new JMenuBar();
     
    		// Mortgage Loans menu
    		JMenu loanMenu = new JMenu("Loan Terms Menu");
    		menuBar.add(loanMenu);
     
    		// add menu items
    		JMenuItem term7MenuItem = new JMenuItem("7 year at 5.35%");
    		JMenuItem term15MenuItem = new JMenuItem("15 year at 5.5%");
    		JMenuItem term30MenuItem = new JMenuItem("30 year at 5.75%");
    		JMenuItem quitMenuItem = new JMenuItem("Quit");
     
    		loanMenu.add(term7MenuItem);
    		loanMenu.add(term15MenuItem);
    		loanMenu.add(term30MenuItem);
    		loanMenu.addSeparator();
    		loanMenu.add(quitMenuItem);
     
    		setJMenuBar(menuBar);
     
    				// create panel
    		JPanel panel = new JPanel(new FlowLayout());
    		// add label and text field
    		panel.add(label);
    		panel.add(amountText);
     
    		// add panel at north
    		container.add(panel, BorderLayout.NORTH);
     
    		// create output text area and make it not editable
    		// and set font as "Courier New"
    		outputTextArea = new JTextArea();
    		outputTextArea.setEditable(false);
    		outputTextArea.setFont(new Font("Courier New", Font.PLAIN, 12));
     
    		// and output text area at the center
    		container.add(new JScrollPane(outputTextArea), BorderLayout.CENTER);
     
    		// 7 years at 5.35% loan menu item click event listener
    		term7MenuItem.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				displayLoan(terms[0], rates[0]);
    			}
    		});
     
    		// 15 years at 5.5% loan menu item click event listener
    		term15MenuItem.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				displayLoan(terms[1], rates[1]);
    			}
    		});
     
    		// 30 years at 5.75% loan menu item click event listener
    		term30MenuItem.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				displayLoan(terms[2], rates[2]);
    			}
    		});
     
    		// quit menu item click event listener
    		quitMenuItem.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				if (JOptionPane.showConfirmDialog(null, "Do you really want to quit?", "Quit", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
    				{
    					System.exit(0);
    				}
    			}
    		});
    	}
     
    	// Display the mortgage payment amount followed by the loan balance and
    	// interest paid for each payment over the term of the loan.
    	public void displayLoan(int term, double rate)
    	{
    		double amount;            // loan amount
    		int years;                // number of years
    		int months;               // total months
    		double monthlyRate;       // monthly interest rate
    		double monthlyPayment;    // monthly mortgage payment amount
    		NumberFormat nf = NumberFormat.getCurrencyInstance();
    		double loanBalance;
    		double interestPaid;
     
    		try
    		{
    			amount = Double.parseDouble(amountText.getText());
    		}
    		catch (Exception ex)
    		{
    			JOptionPane.showMessageDialog(null, "Amount must be numeric", "Error", JOptionPane.ERROR_MESSAGE);
    			return;
    		}
     
    		if (amount <= 0)
    		{
    			JOptionPane.showMessageDialog(null, "Amount must be positive", "Amount", JOptionPane.ERROR_MESSAGE);
    			return;
    		}
     
    		// calculate total months
    		months = term * 12;
    		// calculate monthly interest rate
    		monthlyRate = rate / 1200;
    		// calculate monthly mortgate payment amount
    		monthlyPayment = (amount * monthlyRate) / (1.0 - (Math.pow(1.0 + monthlyRate, -months)));
    		// set mortgage payment amount in output text area
    		outputTextArea.setText(String.format("Monthly payment amount: %s\n\n", nf.format(monthlyPayment)));
     
    		// append header for Month, Loan Balance, and Interest Paid
    		outputTextArea.append(String.format("%10s %20s %20s\n", "Month", "Loan Balance", "Interest Paid"));
    		outputTextArea.append(String.format("%10s %20s %20s\n", "-----", "------------", "-------------"));
     
    		// for each month, display Month, Loan Balance, and Interest Paid
    		loanBalance = amount;
     
    		for (int month = 1; month <= months; month++)
    		{
    			// calculate monthly interest paid
    			interestPaid = loanBalance * monthlyRate;
    			// calculate loan Balance
    			loanBalance = loanBalance - monthlyPayment + interestPaid;
     
    			outputTextArea.append(String.format("%10d %20s %20s\n", month,
    				nf.format(loanBalance), nf.format(interestPaid)));
    		}
    	}
     
    	public static void main(String args[])
    	{
    		// create Mortgage class object
    		Mortgage mortgage = new Mortgage();
     
    		// set size of window
    		mortgage.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    		// make window exit when closed
    		mortgage.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		// make location of window at center of screen
    		mortgage.setLocationRelativeTo(null);
    		// make window not resizable
    		mortgage.setResizable(false);
    		// make window visible
    		mortgage.setVisible(true);
    	}
    }

    Thanks in advance.

    IKE


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Need Help on Updating a GUI Java

    The first thing you should do is a bit of research on the MVC pattern.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need Help on Updating a GUI Java

    Are there specific questions about the code you posted? Does it compile? Are there exceptions? Its hard to address what you've posted given its not as specific as folks would prefer. Would recommend you try and break the problem down into smaller components, work on each one at a time and post specific questions about those issues.
    Not exactly sure what I need to do to make it do this.
    You can make it do what you want Just a matter of playing with the code over and over.

    Quote Originally Posted by junky
    The first thing you should do is a bit of research on the MVC pattern.
    While MVC is an important concept, it is also advanced, subject to interpretation, and has many ways to implement. I encourage the original poster to research it, with the idea that it probably won't be the end all be all answer to fix your problems.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Need Help on Updating a GUI Java

    The very basic concept of the MVC pattern is not that advanced. Don't stick all your code together in the one class.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need Help on Updating a GUI Java

    Quote Originally Posted by Junky View Post
    The very basic concept of the MVC pattern is not that advanced. Don't stick all your code together in the one class.
    In my mind that's not the basic concept of MVC...but your mileage may vary. I still don't see how this addresses the original post, and am just pointing this out so the original poster doesn't go spend time getting confused on yet one more subject that doesn't directly apply to what they are trying to accomplish.

  6. #6
    Junior Member
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help on Updating a GUI Java

    OK let me clarify a bit.

    My code as posted Compiles & runs OK.

    What I need to know is how to make the necessary changes to the current code so I can accomplish what I need to do with it.

    I need the current program as shown to be able to do the following things:
    so that it will allow the user to select which way they want to calculate a mortgage: by input of the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage payment or by input of the amount of a mortgage and then select from a menu of mortgage loans:

    - 7 year at 5.35%
    - 15 year at 5.5 %
    - 30 year at 5.75%

    In either case, display the mortgage payment amount and then, list the loan balance and interest paid for each payment over the term of the loan. Allow the user to loop back and enter a new amount and make a new selection, or quit. Insert comments in the program to document the program


    I just need to know how to modify the current working code to accomplish these tasks.

    Oh and what is MVC, I have never heard of that in the readings I have used for my trials and efforts.

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need Help on Updating a GUI Java

    MVC == Model-View-Controller. Its a design pattern meant to separate components which perform different tasks. Java Swing actually uses this pattern in many contexts, take for example a table: a JTable the viewer, the TableModel the model, and any sort of Listener (MouseListener) the controller. It is not limited to Swing however, enterprise/server applications make (or at least should make) use of this pattern. Its basically meant to make to separate functionality, reduce dependencies, and help make projects easier to maintain and, like most design patterns, its easy to read about but harder to implement correctly.

    What is your code missing right now? On quick glance it seems to have the values hardcoded already, as well as the calculations...like I said above, break the problem down into individual components of what you don't have/what you need and work one step at a time. Questions such as "how do I display a value to the user?" are much easier to answer than "here are my requirements, how do I do it?"

  8. #8
    Junior Member
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help on Updating a GUI Java

    OK Copeg,
    The first thing I need to figure out is how to modify the gui so that users can input the values for term in months, Rate in percent, & loan amount.
    So I guess I need to modify the code to create 3 text boxes that can take various values

  9. #9
    Junior Member
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help on Updating a GUI Java

    hmmm still waiting on feedback on this topic.

    THanks all

  10. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need Help on Updating a GUI Java

    Have you read the following tutorial? If not, it might answer how you can accomplish your requirements How to Use Text Fields (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

Similar Threads

  1. Updating timer
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 26th, 2011, 07:02 AM
  2. Updating GUI dynamically
    By KrisTheSavage in forum AWT / Java Swing
    Replies: 2
    Last Post: August 29th, 2010, 08:23 AM
  3. MouseMotionListener is not updating my variable
    By olemagro in forum AWT / Java Swing
    Replies: 1
    Last Post: February 8th, 2010, 03:44 PM
  4. updating EDT with thread swingworker/invokeLater ?
    By mdstrauss in forum AWT / Java Swing
    Replies: 0
    Last Post: October 11th, 2009, 04:52 AM
  5. updating database
    By gurpreetm13 in forum JDBC & Databases
    Replies: 3
    Last Post: October 9th, 2009, 11:43 AM