Help! Java Program w/GUI


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Help! Java Program w/GUI

  1. #1
    Join Date
    Jan 2007
    Location
    Pennsylvania
    Posts
    14

    Help! Java Program w/GUI

    Hello everyone. I am sooo lost on this assignment for my class. I have been messing around with this for days, and just cant seem to understand what I have to do. The teacher is not willing to help, and everyone else in my class is struggling with the same issues. The program needs to allow the user to enter the principle amount of the loan in a textfield, and either choose from the dropdown lists for the interest and term of the loan or allow the user to enter the interest and term in a text field. In the coding below I have added the text fields for the interest and term of the loan, but the dropdown lists are interferring and not allowing the entered data to be calculated instead. Additionally, I cant seem to organize the information in the window either. I wanted to get the text fields next to the associated label. The dropdown lists are next to the correct label, but the text fields are not. Additionally, I have to implement a scrolling pane that contains the principle balance and interest paid for each payment over the term of the loan. I have no idea how to complete these things, and am very frustrated. If anyone can help me at all, it will be greatly appreciated. I really want to learn these things, but just cant seem to grasp anything without actually seeing it done. Please help!! Thank you in advance for everything. Below is my coding thus far:

    Code:
    /
    /*Java Package Imports*/
    import javax.swing.JOptionPane;
    import javax.swing.JComponent;
    import javax.swing.*;
    import javax.swing.JComboBox;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;
    
    public class MortgageCalculatorGUI4 extends Frame implements ActionListener, ItemListener
    {
    	/*Set background color of panel or screen*/
    	Color lightBlue = new Color(163,187,255);
    
    	/*Declaring and initializing variables*/
    	int term = 0;
    	double interestRate = 0;
    	double principle = 0;
    	double payment = 0;
    	String ArrayTerm [] = {"7", "15", "30"};
    	String ArrayInterestRate [] = {"5.35", "5.5", "5.75"};
    
    	/*Established control buttons*/
    	Panel buttonPanel = new Panel();
    	Button resetButton = new Button("Reset");
    	Button calculateButton = new Button("Calculate");
    	Button cancelButton = new Button("Cancel");
    
    	/*Established fields*/
    	Panel inputPanel = new Panel();
    	Label loanPrinciple = new Label("Loan Principle: $");
    	TextField textPrinciple = new TextField(12);
    	Label loanTerm = new Label("Term of Loan (Years): ");
    	TextField textTerm = new TextField(3);
    	Label loanInterest = new Label("Annual Interest Rate (Percentage): ");
    	TextField textInterest = new TextField(6);
    	Panel comboBox = new Panel();
    	JComboBox interestRateList, termList;
    
    	public MortgageCalculatorGUI4()
    	{
    		/*Setup data entry screen*/
    		this.setLayout(new BorderLayout());
    		this.setBackground(lightBlue);
    		buttonPanel.setLayout(new FlowLayout());
    		inputPanel.setLayout(new FlowLayout());
    
    		/*Setup data entry screen fields*/
    		inputPanel.add(loanPrinciple);
    		inputPanel.add(textPrinciple);
    		inputPanel.add(loanTerm);
    		inputPanel.add(textTerm);
    		inputPanel.add(loanInterest);
    		inputPanel.add(textInterest);
    
    
    		/*Buttons to click with mouse*/
    		buttonPanel.add(resetButton);
    		buttonPanel.add(calculateButton);
    		buttonPanel.add(cancelButton);
    		buttonPanel.setBackground(lightBlue);
    
    		/*Created lists through an array*/
    		termList = new JComboBox(ArrayTerm);
    		termList.setSelectedIndex(0);
    		termList.addItemListener(this);
    
    		interestRateList = new JComboBox(ArrayInterestRate);
    		interestRateList.setSelectedIndex(0);
    		interestRateList.addItemListener(this);
    
    		/*Setup lists*/
    		comboBox.add(loanTerm);
    		comboBox.add(termList);
    		comboBox.add(loanInterest);
    		comboBox.add(interestRateList);
    
    		/*Add location of data entry fields to panel*/
    		add(inputPanel, BorderLayout.NORTH);
    		/*Add buttons to panel, reset, calculate, and cancel*/
    		add(buttonPanel, BorderLayout.SOUTH);
    		/*Add list to panel*/
    		add(comboBox, BorderLayout.EAST);
    
    		/*Activate button listening*/
    		resetButton.addActionListener(this);
    		calculateButton.addActionListener(this);
    		cancelButton.addActionListener(this);
    
    
    	}
    
    	/*Established display*/
    	public static void main(String[] args)
    	{
    		MortgageCalculatorGUI4 frame = new MortgageCalculatorGUI4();
    		frame.setBounds(200,200,750,300);
    		frame.setTitle("Mortgage Calculator GUI3");
    		frame.setVisible(true);
    
    	}
    
    
    	public void itemStateChanged(ItemEvent e)
    	{
    		/*Gathering information from selections*/
    		if(e.getStateChange() == ItemEvent.SELECTED)
    		{
    			if(e.getSource() == termList)
    			{
    				int pos = termList.getSelectedIndex();
    				interestRateList.setSelectedIndex(pos);
    			}
    			if(e.getSource() == interestRateList)
    			{
    				int pos = interestRateList.getSelectedIndex();
    				termList.setSelectedIndex(pos);
    			}
    		}
    	}
    
    	/*Resets entries*/
    	public void actionPerformed(ActionEvent e)
    	{
    		String arg = e.getActionCommand();
    
    		/*Resets fields on panel*/
    		if (arg == "Reset")
    		{
    			textPrinciple.setText("");
    			textTerm.setText("");
    			textInterest.setText("");
    			arg = "";
    		}
    
    		/*Canels application*/
    		if (arg == "Cancel")
    		{
    			System.exit(0);
    			arg="";
    		}
    
    		/*Performs calculations on entered data*/
    		if (arg == "Calculate")
    		{
    			DecimalFormat TwoDigits = new DecimalFormat("###,###,###.##");
    			double numberOfPayments=0;
    			double principleValue=0;
    			double interestRate=0;
    			try
    			{
    				principleValue = Double.parseDouble(textPrinciple.getText());
    				try
    				{
    					numberOfPayments = Double.parseDouble((String)termList.getSelectedItem())*12;
    				}
    				finally
    				{
    					interestRate = Double.parseDouble((String)interestRateList.getSelectedItem())/100/12;
    				}
    			}
    			catch (Exception ex)
    			{
    				clearFields();
    			}
    			
    			/*Payment calculation*/
    			double payment = (principleValue * interestRate) / (1 - Math.pow(1 + interestRate, - numberOfPayments));
    
    			/*Output displaying Principle, years, and interest entered and payment and number of months calculated*/
    			JOptionPane.showMessageDialog(null," Principle: $"+ TwoDigits.format(principleValue) +
    			" Interest Rate: " + TwoDigits.format(interestRate*100*12) +"% Term of Loan: "+ numberOfPayments/12 + " year(s) " +
    			"or "+ numberOfPayments + " monthly payments of $" + TwoDigits.format(payment)+
    			" totalling $"+TwoDigits.format(payment*numberOfPayments)+". ","Mortgage Calculator GUI4",JOptionPane.INFORMATION_MESSAGE);
    			arg="";
    		}
    
    	}
    
    	public void clearFields()
    	{
    		textPrinciple.setText("");
    		textTerm.setText("");
    		textInterest.setText("");
    	}
    
    }/
    Thanks,
    Nicole
    Nicole

  2. #2
    Join Date
    Dec 2005
    Posts
    97
    ok well i think i have at least the interface set up more like you need it to be. and then to calculate what the user enters into the textfields just do something like

    if user selected "Enter own value" from the list do the calculation using the textfields
    else do the calculation using the list values (what you already have done)

    Additionally, I have to implement a scrolling pane that contains the principle balance and interest paid for each payment over the term of the loan.
    have you just never using a scrollpane or you just cant figure out what to do?

    Code:
    /*Java Package Imports*/
    import javax.swing.JOptionPane;
    import javax.swing.JComponent;
    import javax.swing.*;
    import javax.swing.JComboBox;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;
    
    public class MortgageCalculatorGUI4 extends Frame implements ActionListener, ItemListener
    {
    	/*Set background color of panel or screen*/
    	Color lightBlue = new Color(163,187,255);
    
    	/*Declaring and initializing variables*/
    	int term = 0;
    	double interestRate = 0;
    	double principle = 0;
    	double payment = 0;
    	String ArrayTerm [] = {"7", "15", "30", "Enter own value"};
    	String ArrayInterestRate [] = {"5.35", "5.5", "5.75", "Enter own value"};
    
    	/*Established control buttons*/
    	Panel buttonPanel = new Panel();
    	Button resetButton = new Button("Reset");
    	Button calculateButton = new Button("Calculate");
    	Button cancelButton = new Button("Cancel");	
    
    	/*Established fields*/
    	Panel inputPanel = new Panel();
    	Label loanPrinciple = new Label("Loan Principle: $");
    	TextField textPrinciple = new TextField(12);
    	Label loanTerm = new Label("Term of Loan (Years): ");
    	TextField textTerm = new TextField(3);
    	Label loanInterest = new Label("Annual Interest Rate (Percentage): ");
    	TextField textInterest = new TextField(6);
    	Panel comboBox = new Panel();
    	JComboBox interestRateList, termList;
    
    	public MortgageCalculatorGUI4()
    	{
    		/*Setup data entry screen*/
    		this.setLayout(new BorderLayout());
    		this.setBackground(lightBlue);
    		buttonPanel.setLayout(new FlowLayout());
    		inputPanel.setLayout(new FlowLayout());
            
    		/*Setup data entry screen fields*/
    		inputPanel.add(loanPrinciple);
    		inputPanel.add(textPrinciple);
    		inputPanel.add(loanTerm);		
    		inputPanel.add(loanInterest);
    
    		/*Buttons to click with mouse*/
    		buttonPanel.add(resetButton);
    		buttonPanel.add(calculateButton);
    		buttonPanel.add(cancelButton);
    		buttonPanel.setBackground(lightBlue);
    
    		/*Created lists through an array*/
    		termList = new JComboBox(ArrayTerm);
    		termList.setSelectedIndex(0);
    		termList.addItemListener(this);
    
    		interestRateList = new JComboBox(ArrayInterestRate);
    		interestRateList.setSelectedIndex(0);
    		interestRateList.addItemListener(this);
    
    		/*Setup lists*/
    		comboBox.add(loanTerm);
    		comboBox.add(termList);
    		comboBox.add(textTerm);
    		comboBox.add(loanInterest);
    		comboBox.add(interestRateList);		
    		comboBox.add(textInterest);	
    		
    		textTerm.setVisible(false);
    		textInterest.setVisible(false);	
    
    		/*Add location of data entry fields to panel*/
    		add(inputPanel, BorderLayout.NORTH);
    		/*Add buttons to panel, reset, calculate, and cancel*/
    		add(buttonPanel, BorderLayout.SOUTH);
    		/*Add list to panel*/
    		add(comboBox, BorderLayout.CENTER);
    
    		/*Activate button listening*/
    		resetButton.addActionListener(this);
    		calculateButton.addActionListener(this);
    		cancelButton.addActionListener(this);
    
    
    	}
    
    	/*Established display*/
    	public static void main(String[] args)
    	{
    		MortgageCalculatorGUI4 frame = new MortgageCalculatorGUI4();
    		frame.setBounds(200,200,750,300);
    		frame.setTitle("Mortgage Calculator GUI3");
    		frame.setVisible(true);
    
    	}
    
    
    	public void itemStateChanged(ItemEvent e)
    	{
    		/*Gathering information from selections*/
    		if(e.getStateChange() == ItemEvent.SELECTED)
    		{
    			if(e.getSource() == termList)
    			{				
    				int pos = termList.getSelectedIndex();
    				if(pos == 3)
    				{
    					textTerm.setVisible(true);
    					textInterest.setVisible(true);					
    					this.show();
    					
    				}
    				else
    				{
    					textTerm.setVisible(false);
    					textInterest.setVisible(false);
    					this.show();
    				}
    					
    				interestRateList.setSelectedIndex(pos);
    			}
    			if(e.getSource() == interestRateList)
    			{
    				int pos = interestRateList.getSelectedIndex();
    				termList.setSelectedIndex(pos);
    			}
    		}
    	}
    
    	/*Resets entries*/
    	public void actionPerformed(ActionEvent e)
    	{
    		String arg = e.getActionCommand();
    
    		/*Resets fields on panel*/
    		if (arg == "Reset")
    		{
    			textPrinciple.setText("");
    			textTerm.setText("");
    			textInterest.setText("");
    			arg = "";
    		}
    
    		/*Canels application*/
    		if (arg == "Cancel")
    		{
    			System.exit(0);
    			arg="";
    		}
    
    		/*Performs calculations on entered data*/
    		if (arg == "Calculate")
    		{
    			DecimalFormat TwoDigits = new DecimalFormat("###,###,###.##");
    			double numberOfPayments=0;
    			double principleValue=0;
    			double interestRate=0;
    			try
    			{
    				principleValue = Double.parseDouble(textPrinciple.getText());
    				try
    				{
    					numberOfPayments = Double.parseDouble((String)termList.getSelectedItem())*12;
    				}
    				finally
    				{
    					interestRate = Double.parseDouble((String)interestRateList.getSelectedItem())/100/12;
    				}
    			}
    			catch (Exception ex)
    			{
    				clearFields();
    			}
    			
    			/*Payment calculation*/
    			double payment = (principleValue * interestRate) / (1 - Math.pow(1 + interestRate, - numberOfPayments));
    
    			/*Output displaying Principle, years, and interest entered and payment and number of months calculated*/
    			JOptionPane.showMessageDialog(null," Principle: $"+ TwoDigits.format(principleValue) +
    			" Interest Rate: " + TwoDigits.format(interestRate*100*12) +"% Term of Loan: "+ numberOfPayments/12 + " year(s) " +
    			"or "+ numberOfPayments + " monthly payments of $" + TwoDigits.format(payment)+
    			" totalling $"+TwoDigits.format(payment*numberOfPayments)+". ","Mortgage Calculator GUI4",JOptionPane.INFORMATION_MESSAGE);
    			arg="";
    		}
    
    	}
    
    	public void clearFields()
    	{
    		textPrinciple.setText("");
    		textTerm.setText("");
    		textInterest.setText("");
    	}
    
    }

  3. #3
    Join Date
    Jan 2007
    Location
    Pennsylvania
    Posts
    14
    Nope, never designed a scrolling pane in a GUI before. I have coding that i used for the console window, but am not sure if this is able to be used in the coding for this assignment. I am assuming it can be...below is the coding that was used before, but I do not think I am to use the counter. I will try to work on the calculations part in what you have done for me....this is perfect . Thank you.

    Code:
     /
    *Added looping (counter) and calculations*
     /*Added looping (counter) and calculations*/
            while (cntr<4)
            	{
            	if (cntr==1)
            		{
            		term=7;
            		principle=200000.00;
            		interest=5.35;
            		}
            	if (cntr==2)
            		{
            		term=15;
            		principle=200000.00;
            		interest=5.5;
            		}
            	if (cntr==3)
        			{
            		term=30;
            		principle=200000.00;
            		interest=5.75;
        			}
        		/*Calculation Formulas for the loan payment, balance, and principle*/
            	NP = term*12;
            	IR = interest/100/12;
            	PV = principle;
            	/*Counters and array*/
            	mortgages[1][cntr] = PV;
            	mortgages[2][cntr] = interest;
               	mortgages[3][cntr] = term;
               	mortgages[4][cntr] = (PV * IR) / (1 - Math.pow(1 + IR, - NP));   //Calculation for monthly payment
                mortgages[5][cntr] = NP;
               	mortgages[6][cntr] = mortgages[4][cntr]*mortgages[5][cntr];
    			double mInterest, mBalance, mPayment, payment, balance, mPrinciple;
    			double counter1=0;
    			double cntr2=0;
    			mInterest = 0.00;
    			mPayment=0.00;
    			mBalance=0.00;
    			payment = mortgages[4][cntr];
    			balance = mortgages[6][cntr];
    			mPrinciple=mortgages[1][cntr];
    			System.out.println("Month\tPrinciple\tInterest\tPay-to-Principle\tPayment\t\tBalance");
    			while (counter1<NP)
    			{
    				counter1++;
                	cntr2++;
                	mInterest=IR*mPrinciple;
                	mPayment=payment-mInterest;
                	mBalance=mPrinciple+mInterest-payment;
                	//Output message--creates title line for listing payments, balance, and interest.
                	System.out.println("\n"+counter.format(counter1)+"\t"+twoDigits.format(mPrinciple)+"\t"+twoDigits.format(mInterest)+"\t\t"+twoDigits.format(mPayment)+"\t\t\t"+twoDigits.format(payment)+"\t"+twoDigits.format(mBalance));
                	mPrinciple=mBalance;
    
                if (cntr2==12)
                {
    				System.out.println("\n\t\t"+counter.format(counter1/12)+" years processed. Pausing 5 seconds...");
    			    try
    			    {
    					/*asked currently executing Thread to sleep or pause for 5000ms (5 seconds)*/
    			        Thread.sleep(5000);
    			        }
    			        catch(InterruptedException e)
    			        {
    			        System.out.println("Sleep interrupted:"+e);
    			        }
    			        cntr2=0;
    			        }
                	}
    
    /
    Nicole

  4. #4
    Join Date
    Dec 2005
    Posts
    97

  5. #5
    Join Date
    Jan 2007
    Location
    Pennsylvania
    Posts
    14
    As far as the output received, is there a way to get just the monthly payment to appear in the original pane, eliminate the popup window entirely? The rest of the information needed is to be in the scrolling pane. Any ideas on this?

    Thanks,
    Nicole
    Nicole

Similar Threads

  1. Java book
    By Lou in forum Java
    Replies: 9
    Last Post: 09-19-2007, 06:58 AM
  2. Help! Simple Java Program.
    By SlickWilly440 in forum Java
    Replies: 2
    Last Post: 01-28-2006, 10:17 PM
  3. DevX does seem one sideded
    By Rob Abbe in forum Talk to the Editors
    Replies: 44
    Last Post: 01-13-2003, 03:57 PM
  4. Has Sun Given Up on the Desktop?
    By Lori Piquet in forum Talk to the Editors
    Replies: 114
    Last Post: 10-10-2002, 07:01 AM
  5. .NET vs. Enterprise Java: Who's Got Better Security?
    By Glen Kunene in forum Talk to the Editors
    Replies: 17
    Last Post: 03-23-2002, 01:43 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