2 Replies - 599 Views - Last Post: 11 January 2009 - 07:24 PM Rate Topic: -----

#1 ken7280  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 01-December 08

descending mortgage GUI

Posted 11 January 2009 - 05:35 PM

I'M TRYING TO CHANGE THIS CODE FROM ASCENDING TO DESCENDING TO PAYMENTS.IT MY ERROR SAYS ARRAY OUT OF BOUNDS. I'VE CHANGED THE FOR LOOP INCREMENTS FROM++ TO --
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.text.*;
import java.io.*;
import javax.swing.text.*;
import javax.swing.JOptionPane;

public class descendingInterest extends JFrame implements
ActionListener {

	JPanel firstRow = new JPanel();
	JPanel secondRow = new JPanel();
	JPanel thirdRow = new JPanel();
	JPanel fourthRow = new JPanel();

	JLabel principalLabel = new JLabel("Enter the Loan Amount to be Calculated:>>>>>>>>>");
	JTextField principal = new JTextField(10);

	JLabel numberLabel = new JLabel("Choose loan to Amortize:>>>>>>>>>>>>>>>>>>>>>");
	JComboBox numberCombo = new JComboBox();

	JTextPane textPane = new JTextPane();

	JButton dispButton = new JButton("Amortization");
	JButton resetButton = new JButton("Reset");
	JButton quitButton = new JButton("Quit");

	JPanel fieldPanel = new JPanel();
	JPanel buttonPanel = new JPanel();
	JPanel schdPanel = new JPanel();
	JTextPane textpane = new JTextPane();
	//JTextpane textpane2 = new JTextPane();

	int termArray[]={7,15,30};
	//public double
	String choiceArray[]={"7 years @5.35%","15 years @ 5.5%","30 years @ 5.75%"};
	double rateArray[] = {.0535, .055, .0575};


	public Container createContentPane()
	{
		int count, inum;
			//set dimensions of window
		setSize(800, 600);

		for (inum=0; inum<3; inum--)
		{
			numberCombo.addItem(choiceArray[inum]);
			//numArray[count] =inum;
		}
			//add items to the combo box
   		numberCombo.addItem("snumString[inum])");
		fieldPanel.setLayout(new GridLayout(2,2));

		principal.setText("");
		fieldPanel.add(principalLabel);
		fieldPanel.add(principal);
		fieldPanel.add(numberLabel);
		fieldPanel.add(numberCombo);

			//for the buttonPanel set the layout to be default, in rows from left
			//to right
		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

		dispButton.setPreferredSize(new Dimension(120, 35));
		resetButton.setPreferredSize(new Dimension(120, 35));
		quitButton.setPreferredSize(new Dimension(120, 35));
		buttonPanel.add(dispButton);
		buttonPanel.add(resetButton);
		buttonPanel.add(quitButton);

			//create tab stops and set font styles for text area
		setTabsAndStyles(textpane);
			//create initial text area
		textpane=addTextToTextPane();
			//add the ability to scroll through the text area
			//use JScrollpane when what to be displayed is very large
			//or dynamically changes
		JScrollPane scrollPane = new JScrollPane(textpane);
			scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
			scrollPane.setPreferredSize(new Dimension(500, 200));
		schdPanel.add(scrollPane);

		Container c = getContentPane();
			c.setLayout(new BorderLayout(20, 20));
			c.add(schdPanel, BorderLayout.SOUTH);
			c.add(buttonPanel, BorderLayout.CENTER);
			c.add(fieldPanel, BorderLayout.NORTH);

		//computeButton.addActionListener(this);
		resetButton.addActionListener(this);
		dispButton.addActionListener(this);
		quitButton.addActionListener(this);

			return c;

	}

	protected void setTabsAndStyles(JTextPane textPane)
	{
		Style fontStyle =
			StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

		Style regular = textPane.addStyle("regular", fontStyle);
		StyleConstants.setFontFamily(fontStyle, "SansSerif");

		Style s = textPane.addStyle("italic", regular);
		StyleConstants.setItalic(s, true);
		s = textPane.addStyle("bold", regular);
		StyleConstants.setBold(s, true);
		s = textPane.addStyle("large", regular);
		StyleConstants.setFontSize(s,16);
	}

	public JTextPane addTextToTextPane()
	{
		Document doc = textPane.getDocument();

		try
		{
				//clear current area
			doc.remove(0,doc.getLength());
				//add title
		}
		catch (BadLocationException ble)
		{
			System.err.println("Cant Insert Text");
		}

				return textPane;
	}


	public void fillInScroll()
	{


		int prtPmtNum, selIdx, selNum, loop_ctr, year_ctr, years;
		double payPerMonth, princAmt, intRate, startBal, endBal, payToInt, payToPrinc;
		String sPrinc;

			//decimal formats
		DecimalFormat twoDigits = new DecimalFormat("0.00");
		java.text.NumberFormat currency = java.text.NumberFormat.getCurrencyInstance();
		 NumberFormat nf = NumberFormat.getInstance();
				nf.setMaximumFractionDigits(2);
				nf.setMinimumFractionDigits(2);

			//get relevant amounts
		sPrinc=principal.getText();
		princAmt=Double.parseDouble(sPrinc);
		selIdx = numberCombo.getSelectedIndex();
		years=termArray[selIdx];
		intRate=rateArray[selIdx];


		JTextPane textPane2 = new JTextPane();



		try
		{
			Document doc = textPane.getDocument();
							//clear current area
			doc.remove(0,doc.getLength());
			doc.insertString(doc.getLength(), "Year #\tStart Amt\tEnd Amt\tInterest\n",textPane.getStyle("regular"));
			startBal=princAmt;
			for (year_ctr = 0; year_ctr < years; year_ctr--)
			{
				endBal = startBal - (startBal * intRate);	//ckk

				payToInt = endBal - startBal;

						//ckk
				doc.insertString(doc.getLength()," " + String.valueOf(year_ctr - 1) + "\t" +
				nf.format(startBal) + "\t" + nf.format(endBal) + "\t" +
				nf.format(payToInt) + "\n",textPane.getStyle("regular"));
				startBal = endBal;
			}



		}
		catch (BadLocationException ble)
		{
			System.err.println("Cant Insert Text");
		}

	}
	public void emptyPane()
	{

		try
		{
			Document doc = textPane.getDocument();
				//clear current area
			doc.remove(0,doc.getLength());

		}
		catch (BadLocationException ble)
		{
			System.err.println("Error blanking scrollable region");
		}

		//return textPane;
	}

		public void actionPerformed(ActionEvent e){
			Object src = e.getSource();

		if (src == resetButton)//Set reset button to take effect
		{
			numberCombo.setSelectedIndex(0);
			emptyPane();
		}
		if (src == quitButton)
		{
			System.exit(0);
		}
		if (src == dispButton)
		{
			fillInScroll();
		}

	}


	public static void main(String args[]) {//initialize application main method

		JFrame.setDefaultLookAndFeelDecorated(true);
		descendingInterest mc = new descendingInterest();
		mc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mc.setContentPane(mc.createContentPane());
		mc.setSize(600,375);
		mc.setVisible(true);

	}
	public descendingInterest()
	{
		super("Example of scrollable region");

	}


}


Edited to correct the [ code][ /code] tags

This post has been edited by pbl: 11 January 2009 - 07:23 PM


Is This A Good Question/Topic? 0
  • +

Replies To: descending mortgage GUI

#2 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: descending mortgage GUI

Posted 11 January 2009 - 05:45 PM

Edit your post to post code like: :code:

Also you cannot just change ++ to -- you have to change the strating condition to the size of the array minus one, and the end condition to >0 or >=1.

Now if you want to start printing out payments starting with the last payment you will have to do all the calculations then print out the calculations going backwards.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: descending mortgage GUI

Posted 11 January 2009 - 07:24 PM

Edited the post to correct the [ code] tags
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1