Results 1 to 13 of 13
  1. #1
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Amortization Schedule Table Help.

    Hi, I am making an Amortization Schedule in java GUI and need help with my JTable. I am trying to make a table with the data stored in it but I don't know the number of rows it needs. Is there any way I can set the total number of rows in the table, or can I base it somehow off of the total number of payments the user is making on their loan. Thank you for your help! Couldn't figure out how to insert code.

    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    public class AmortizationGUI extends JFrame implements ActionListener {
    
    	
    	
    	public AmortizationGUI(){
    		Font font = new Font("Arial",Font.PLAIN,16);
    		Font fieldFont = new Font("Arial", Font.PLAIN,14);
    		//JFrame attributes
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setTitle("Amortization Schedule");
    		setSize(800,800);
    		setLocationRelativeTo(null);
    		setResizable(false);
    		//initialize panel
    		JPanel panel = new JPanel();
    		panel.setLayout(null);
    		
    		
    		
    		//initialize JLabels
    		JLabel lA = new JLabel("Loan Amount:");
    		lA.setLocation(50,50);
    		lA.setFont(font);
    		lA.setSize(100,50);
    		lA.setVisible(true);
    		JLabel iR = new JLabel("Interest Rate:");
    		iR.setSize(100,50);
    		iR.setFont(new Font("Arial",Font.PLAIN,17));
    		iR.setLocation(50,100);
    		iR.setVisible(true);
    		JLabel yL = new JLabel("Years of Loan:");
    		yL.setSize(100,50);
    		yL.setLocation(49,150);
    		yL.setVisible(true);
    		yL.setFont(font);
    		//initialize JTextFields
    		JTextField lAField = new JTextField(40);
    		lAField.setSize(70,20);
    		lAField.setLocation(150,66);
    		lAField.setVisible(true);
    		lAField.setFont(fieldFont);
    		lAField.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    		JTextField iRField = new JTextField(10);
    		iRField.setSize(70,20);
    		iRField.setLocation(150,116);
    		iRField.setVisible(true);
    		iRField.setFont(fieldFont);
    		iRField.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    		JTextField yLField = new JTextField(10);
    		yLField.setSize(70,20);
    		yLField.setLocation(150,166);
    		yLField.setVisible(true);
    		yLField.setFont(fieldFont);
    		yLField.setBorder(BorderFactory.createLineBorder(Color.black));
    		
    		//initialize button
    		JButton calc = new JButton("Calculate");
    		calc.setSize(100,50);
    		calc.setLocation(100,206);
    		calc.setVisible(true);
    		calc.setFont(font);
    		calc.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    		//button ActionListener
    		
    		//initialize table
    		JTable table;
    		
    		String columnNames[] ={"Payment #","Amount Paid","Principal","Interest","Balance"};
    		
    		
    		Object data[][] = {
    				{"","","","",""}
    		};
    		
    		table = new JTable(data,columnNames);
    		table.setPreferredScrollableViewportSize(new Dimension(400,100));
    		table.setFillsViewportHeight(true);
    		
    		JScrollPane scrollPane = new JScrollPane(table);
    		scrollPane.setBounds(250,200,500,500);
    		
    		//adding elements 
    		panel.add(lA);
    		panel.add(iR);
    		panel.add(yL);
    		panel.add(lAField);
    		panel.add(iRField);
    		panel.add(yLField);
    		panel.add(calc);
    		panel.add(scrollPane);
    		
    		
    		panel.setVisible(true);
    		add(panel);
    		
    		
    	}
    
    	
    	
    	
    	public static void main(String[] args){
    		AmortizationGUI gui = new AmortizationGUI();
    		
    		gui.setVisible(true);
    		
    	}
    
    
    
    
    	@Override
    	public void actionPerformed(ActionEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    
    
    
    
    	
    }
    Last edited by pj6444; 01-07-2013 at 02:48 AM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Amortization Schedule Table Help.


  3. #3
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: Amortization Schedule Table Help.

    I think that the way for you to proceed is to use an instance of DefaultTableModel.

    The following code goes into your constructor for AmortizationGUI()

    Java Code:
    // create a TableModel with zero rows
    defaultTableModel = new DefaultTableModel(columnNames,0);
    The columnNames variable above is a Vector of String objects which are the column names, the zero parameter is the initial number of rows. We set this to be zero and add each row as needed.

    You should declare DefaultTableModel as an instance variable because you'll need to be able to access it when the user clicks on the Calculate button and actionPerformed is called. (In fact, your text items will have to be instance variables for the same reason.)

    Then, also in the same constructor, you should create your JTable as follows:

    Java Code:
    // create a JTable with a DefaultTableModel
    table = new JTable(defaultTableModel);
    Then, when the calculate button is pressed and you call your method to generate the amortization schedule, you can call addRow() of DefaultTableModel to add each row to the table. Also remember to put in code to setRowCount to zero for when you first start generating your amortization schedule to clear any previous schedule that was generated.

  4. #4
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: Amortization Schedule Table Help.

    Thank you very much for your answer. But what exactly does the addRow() method do. Does it add rows and then set the data for the columns? Or does it just add a blank row? Also, what do you mean by an instance variable? Also, how would I add in the data if the table has no data array to call from?
    Last edited by pj6444; 01-07-2013 at 11:33 PM. Reason: Forgot Question

  5. #5
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: Amortization Schedule Table Help.

    But what exactly does the addRow() method do.
    It adds a row to the bottom of the TableModel and to the bottom of the JTable. You don't know how many rows to make, so you create your table with zero rows and add them one-at-a-time so you have just as many rows as you need. addRow() also adds the data for each column because it can take either an array of Objects or a Vector of Objects, so you can add a row and add all the data that the row needs all in one step.

    Also, what do you mean by an instance variable?
    Instance variables are available to all non-static methods for objects of the class. In the code below, I created some instance variables to demonstrate how you can create them in a GUI so that they can be created in the constructor yet used elsewhere such as in the actionPerformed() method.

    Java Code:
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    public class SwingExample extends JFrame implements ActionListener {
    	
    	// these are instance variables.  They are accessible throughout the objects created from this class
    	private JButton calculateButton;
    	private JTextField yearsText;
    	private JTextField monthsText;
    	
    	public SwingExample() {
    		setLayout(new FlowLayout());
    		calculateButton = new JButton("Calculate");
    		calculateButton.addActionListener(this);
    		yearsText = new JTextField(6);
    		monthsText = new JTextField(6);
    		add(calculateButton);
    		add(new JLabel("Years"));
    		add(yearsText);
    		add(new JLabel("Monnths"));
    		add(monthsText);
    		pack();
    		setLocationRelativeTo(null);
    		setVisible(true);
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent event) {
    		if (event.getSource() == calculateButton) {
    			int years = Integer.parseInt(yearsText.getText()); // notice how yearsText is available here because it is an instance variable
    			int months = years * 12;
    			monthsText.setText(Integer.toString(months));
    		}
    		
    	}
    	
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				new SwingExample();
    			}
    		});
    	}
    
    }

  6. #6
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: Amortization Schedule Table Help.

    So, how do I add the data to the table when I use the addRow() method. Is it just another parameter or is there another method I need to use? Again, thank you for your help.

  7. #7
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: Amortization Schedule Table Help.

    Here is a link to the documentation for the class DefaultTableModel. It has two methods called addRow() each one will add a row and will add the data for the row too.

    DefaultTableModel (Java Platform SE 6)

  8. #8
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: Amortization Schedule Table Help.

    Ok. Thank you again. I got that figured out, but I am still having issues.

    Java Code:
    public void actionPerformed(ActionEvent event) {
            if(event.getSource()==calc){//calc is button
                //these are for the calculations
                yearsLoan = Integer.parseInt(yLField.getText());
                interestRate = Integer.parseInt(iRField.getText());
                loanAmount = Integer.parseInt(lAField.getText());
                //adds rows for the number of payments
                for(int counter = 0;counter<=12*yearsLoan;counter++){
                    DefaultTableModel.addRow(data);//data is a vector
                }
            }
    When I do this, my program doesn't do anything.
    But when I do this. It works.

    Java Code:
    public void actionPerformed(ActionEvent event) {
            if(event.getSource()==calc){//calc is button
                //adds rows for the number of payments
                for(int counter = 0;counter<=12*yearsLoan;counter++){
                    DefaultTableModel.addRow(data);//data is a vector
                }
            }

  9. #9
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: Amortization Schedule Table Help.

    When I do this, my program doesn't do anything.
    When you say that your program doesn't do anything. Does it compile? The difference that I see between the two versions of actionPerformed() is that in the first one, it gets the values of three text fields: yLField, iRField, and iAField. Are these three variables declared as instance variables? They would have to be or you'd get compiler errors in actionPerformed().

    Also, your code uses an ID "DefaultTableModel". By convention, in Java, IDs that start with a capital letter are class names. This should be an object so the name should be "defaultTableModel", starting with a lower-case "d".

    Also, the variable "data" will need to be set to a new Vector each time through the for-loop so that there will be new data for each line of the amortization table.

    One more thing, it seems like interestRate and loanAmount should be doubles. Then to convert the text to double you would use:

    Double.parseDouble();

    That's all that I can think of for now.

  10. #10
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: Amortization Schedule Table Help.

    What I mean by nothing happening is that when I press the button nothing happens. Normally, when I press the button it adds an appropriate number of rows with correct data, but when using the variables nothing happens.

  11. #11
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: Amortization Schedule Table Help.

    Why don't you post your code again and I'll take a look at it?

    Short, Self Contained, Correct Example

  12. #12
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: Amortization Schedule Table Help.

    Alright, here is my code:

    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Arrays;
    import java.util.Vector;
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
    public class AmortizationGUI extends JFrame implements ActionListener {
    
    //JTextFields
    JTextField iRField;
    JTextField yLField;
    JTextField lAField;
    
    //array/vector
    String columnNames[] ={"Payment #","Amount Paid","Principal","Interest","Balance"};
    Integer[] hello = {1,2,3,4,5};
    Vector data = new Vector(Arrays.asList(hello));
    
    //table
    JTable table;
    
    //Button
    JButton calc;
    
    //labels
    JLabel lA;
    JLabel iR;
    JLabel yL;
    
    //scrollPane
    JScrollPane scrollPane;
    
    //panel
    JPanel panel;
    
    //fonts
    Font font;
    Font fieldFont;
    
    //variables
    double loanAmount = 0;
    double interestRate = 0;
    double yearsLoan = 1;
    
    //TableModel
    DefaultTableModel tModel = new DefaultTableModel(columnNames,0);
    			
    	public AmortizationGUI(){
    		//font settings
    		font = new Font("Arial",Font.PLAIN,16);
    		fieldFont = new Font("Arial", Font.PLAIN,14);
    		
    		//JFrame attributes
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setTitle("Amortization Schedule");
    		setSize(800,800);
    		setLocationRelativeTo(null);
    		setResizable(false);
    		
    		//initialize panel
    		panel = new JPanel();
    		panel.setLayout(null);
    		
    		
    		
    		//initialize JLabels
    		lA = new JLabel("Loan Amount:");
    		lA.setLocation(50,50);
    		lA.setFont(font);
    		lA.setSize(100,50);
    		lA.setVisible(true);
    		iR = new JLabel("Interest Rate:");
    		iR.setSize(100,50);
    		iR.setFont(new Font("Arial",Font.PLAIN,17));
    		iR.setLocation(50,100);
    		iR.setVisible(true);
    		yL = new JLabel("Years of Loan:");
    		yL.setSize(100,50);
    		yL.setLocation(49,150);
    		yL.setVisible(true);
    		yL.setFont(font);
    		
    		//initialize JTextFields
    		lAField = new JTextField(40);
    		lAField.setSize(70,20);
    		lAField.setLocation(150,66);
    		lAField.setVisible(true);
    		lAField.setFont(fieldFont);
    		lAField.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    		iRField = new JTextField(10);
    		iRField.setSize(70,20);
    		iRField.setLocation(150,116);
    		iRField.setVisible(true);
    		iRField.setFont(fieldFont);
    		iRField.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    		yLField = new JTextField(10);
    		yLField.setSize(70,20);
    		yLField.setLocation(150,166);
    		yLField.setVisible(true);
    		yLField.setFont(fieldFont);
    		yLField.setBorder(BorderFactory.createLineBorder(Color.black));
    		
    		//initialize button
    		calc = new JButton("Calculate");
    		calc.setSize(100,50);
    		calc.setLocation(100,206);
    		calc.setVisible(true);
    		calc.setFont(font);
    		calc.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    		calc.addActionListener(this);
    		
    		//table settings
    		table = new JTable(tModel);
    		table.setPreferredScrollableViewportSize(new Dimension(400,100));
    		table.setFillsViewportHeight(true);
    		
    		//ScrollPane settings
    		scrollPane = new JScrollPane(table);
    		scrollPane.setBounds(250,200,500,500);
    		
    		//adding elements 
    		panel.add(lA);
    		panel.add(iR);
    		panel.add(yL);
    		panel.add(lAField);
    		panel.add(iRField);
    		panel.add(yLField);
    		panel.add(calc);
    		panel.add(scrollPane);
    		
    		panel.setVisible(true);
    		add(panel);
    			
    	}
    
    	//main method
    	public static void main(String[] args){
    		AmortizationGUI gui = new AmortizationGUI();
    		
    		gui.setVisible(true);
    		
    	}
    
    //ActionListener
    	@Override
    	public void actionPerformed(ActionEvent event) {
    		if(event.getSource()==calc){//calc is button
    			//these are for the calculations
    			yearsLoan = Double.parseDouble(yLField.getText());
    			interestRate = Double.parseDouble(iRField.getText());
    			loanAmount = Double.parseDouble(lAField.getText());
    			//adds rows for the number of payments
    			for(int counter = 0;counter<=12*yearsLoan;counter++){
    				tModel.addRow(data);//data is a vector
    			}
    		}
    			
    	}
    
    }
    Thanks again!

  13. #13
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: Amortization Schedule Table Help.

    1. I noticed that the number of periods in your for-loop is from zero to the number of months, which gives one more period than you want.
    2. I think that the variable, yearsLoan, should be an int rather than a double.
    3. You need to be able to create a new Vector each time through the loop with that iteration's data in it. You could do something like:

    Vector<String> data = getData(paymentNumber, amountPaid, principal, interest, balance);

    And then within getData() it would create a Vector and add each parameter after converting it to a String

    I hope that give you an idea of what to do next.

Similar Threads

  1. Programming NFL Schedule
    By PhilArmstrong in forum New To Java
    Replies: 0
    Last Post: 03-04-2010, 02:52 PM
  2. Java Schedule program
    By jaysyshaw in forum New To Java
    Replies: 0
    Last Post: 01-13-2010, 12:07 AM
  3. How to schedule a job in java
    By Malathi in forum Web Frameworks
    Replies: 4
    Last Post: 03-27-2009, 10:14 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •