Jump to content

Help with JTable (Swing)

- - - - -

  • Please log in to reply
3 replies to this topic

#1
msummons76

msummons76

    Newbie

  • Members
  • Pip
  • 7 posts
I am trying to put together a mortgage calculator app that will calculate/show a monthly payment based on the entered loan amount with interest and loan term based on combo box. I am also trying to build a table based on a calculated amortization schedule. This is currently where the problem lies. I am able to build the initial table with 0 values, but am not able to populate the table with the actual amortization calculations. Hopefully there is someone out there who can help me with this as I am no longer able to come up with a logical explaination of my own.....code below:


import javax.swing.*;

import javax.swing.table.*;

import java.awt.*;

import java.awt.event.*;

import java.text.*;

import java.util.*;


public class MikeSummonsMortgageCalculatorAppWK3 extends JFrame

{

	public static void main(String [] args)

	{

		new MikeSummonsMortgageCalculatorAppWK3();

	}

	private JButton button1, button2;

	private JTextField textName1, textName2;

	private JLabel label1, label2;

	private JComboBox combo1; 

	private JTable table;

	private JScrollPane scrollPane;

	private String columnNames[];

	private String dataValues[][];

	

	public MikeSummonsMortgageCalculatorAppWK3()

	{

		//construct Frame

		this.setSize(480,530);

		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		this.setTitle("Mike Summons Mortgage Calculator - Week3");

		

		ButtonListener bl = new ButtonListener();

		

		JPanel panel1 = new JPanel();

		//panel1.setLayout(new FlowLayout(FlowLayout.LEFT));

		

		//add Loan Amount

		label1 = new JLabel("Loan Amount");

		//label1.setLayout(new BorderLayout(BorderLayout.NORTH));

		panel1.add(label1);

		textName1 = new JTextField(15);

		//textName1.setLayout(new BorderLayout(BorderLayout.NORTH));

		panel1.add(textName1);

		

		//add Loan Interest

		combo1 = new JComboBox();

		combo1.addItem("7 year at 5.35%");

		combo1.addItem("15 year at 5.50%");

		combo1.addItem("30 year at 5.75%");

		//combo1.setLayout(new BorderLayout(BorderLayout.CENTER));

		panel1.add(combo1);

		

		//add Monthly Payment

		label2 = new JLabel("Monthly Payment");

		//label2.setLayout(new BorderLayout(BorderLayout.SOUTH));

		panel1.add(label2);

		textName2 = new JTextField(15);

		//textName2.setLayout(new BorderLayout(BorderLayout.SOUTH));

		panel1.add(textName2);

		

		//add Calc button

		button1 = new JButton("Calculate");

		button1.addActionListener(bl);

		panel1.add(button1);

		

		//add Reset button

		button2 = new JButton("Reset");

		button2.addActionListener(bl);

		panel1.add(button2);

		

		String columnNames[] = {"Loan Payment", "Loan Balance", "Interest Paid"};

		

		double a = 0;

		double b = 0;

		double c = 0;

		

		String dataValues[][] =

		{

			{(String.valueOf(a)), (String.valueOf(b)), (String.valueOf(c))}

		};

		table = new JTable (dataValues, columnNames);

		table.setShowHorizontalLines(false);

		table.setRowSelectionAllowed(true);

		table.setColumnSelectionAllowed(true);

		table.setSelectionForeground(Color.white);

		table.setSelectionBackground(Color.red);

		scrollPane = new JScrollPane(table);

		panel1.add(scrollPane, BorderLayout.CENTER);

		

		this.add(panel1);

		

		this.setVisible(true);

	}

	//add button actions

	private class ButtonListener implements ActionListener

	{

		public void actionPerformed(ActionEvent e)

		{

			if (e.getSource() == button1)

			{

				int loanamount = 0;

				

				int[] loanyears;

				loanyears = new int[3];

				loanyears[0] = 7;

				loanyears[1] = 15;

				loanyears[2] = 30;

				

				double[] loaninterest;

				loaninterest = new double[3];

				loaninterest[0] = 5.35;

				loaninterest[1] = 5.50;

				loaninterest[2] = 5.75;

				

				int i = 0;

				

				NumberFormat cf = NumberFormat.getCurrencyInstance();

				

				String testAmt = textName1.getText();

				if (testAmt.length() != 0)

				{

					loanamount = Integer.parseInt(textName1.getText());

				}

				else

				{

					textName1.setText(String.valueOf("Error: No Loan Amount"));

					textName1.requestFocus();

				}

				

				String testRate = (String)combo1.getSelectedItem();

				if (testRate.equals("7 year at 5.35%"))

				{

					i = 0;

				}

				else if (testRate.equals("15 year at 5.50%"))

				{

					i = 1;

				}

				else if (testRate.equals("30 year at 5.75%"))

				{

					i = 2;

				}				

		

				//Declare/Create calculated variables

				double monthlyinterest = loaninterest[i] / 100 / 12;

				int totalpayments = loanyears[i] * 12;

				double monthlypayment = (loanamount * monthlyinterest) / (1 - Math.pow(1 / (1 + monthlyinterest), totalpayments));

		

				//Output calculated monthly payment

				textName2.setText(String.valueOf(cf.format(monthlypayment)));

				

				//Create column values

				//Table code

				for (int n = 0; n < loaninterest.length; n++)

				{

					double loanbalance;

					double loanbalance2;

					double interestpaid = loanamount * monthlyinterest;

				

					//* Output loan balance/interest paid

					loanbalance = loanamount + (loanamount * monthlyinterest);

				

					while (loanbalance > 0.00)

					{

						loanbalance2 = loanbalance - monthlypayment;

						

							String dataValues[][] =

							{

								{(String.valueOf(monthlypayment)), (String.valueOf(loanbalance2)), (String.valueOf(interestpaid))}

							};



							interestpaid = loanbalance2 * monthlyinterest;

							loanbalance = loanbalance2;

							loanbalance = loanbalance + (loanbalance * monthlyinterest);	

					}	

					//end table code

						

				}

			}	

			else if (e.getSource() == button2)

			{

				textName1.setText(String.valueOf(""));

				textName2.setText(String.valueOf(""));

			}			

		}

	}

}




---------- Post added at 03:17 PM ---------- Previous post was at 02:47 PM ----------

To narrow it down, I believe the problem lies with the dataValues variable. I declare its value as 0 to get the table itself up but can not figure out how to change the value of dataValues once the ButtonListener code goes to work.

#2
smithi

smithi

    Newbie

  • Members
  • Pip
  • 9 posts
You could use DefaultTableModel, adding the following rows:

- when you create the table:

DefaultTableModel tableModel = new DefaultTableModel(dataValues, columnNames);

table = new JTable();

table.setModel(tableModel);


- in the ButtonListener of button1, after the definition of the array dataValues

tableModel.addRow(dataValues[0]);


- in the ButtonListener of button2:

int rows_no = tableModel.getRowCount();

for(int i = 0; i < rows_no; i++)

	tableModel.removeRow(0);



#3
msummons76

msummons76

    Newbie

  • Members
  • Pip
  • 7 posts
Thanks for the reply! will try it out and let you know.....

#4
msummons76

msummons76

    Newbie

  • Members
  • Pip
  • 7 posts
Applied the code and it did the job!! Many thanks!!! Suprisingly most Java books out there do not cover much on Jtable. Originally had no idea JTable just displays data and that you need to also implement a table model in order to manipulate table data.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users